gpt4 book ai didi

c# - 在 C# 中设置 slider 值

转载 作者:行者123 更新时间:2023-11-30 23:17:54 30 4
gpt4 key购买 nike

我正在尝试通过 C# 代码设置 slider 值。

我有一个 Main.scene,其中包含一个名为 Slider 的 slider 。主摄像头附有游戏管理器脚本。

slider 不可交互(因此我可以显示值但用户无法更改它)。

我定义了一个变量:

[SerializeField] private Slider sliderObj;     

然后将我的 slider 放入该字段的检查器中
我有一个 float s 变量,我想在 slider 中反射(reflect):

s = player1Time / (player1Time + player2Time) * 100.0f; 

但是当我写的时候:

sliderObj.value = s;

我看到这个错误:

'Slider' does not contain a definition for 'value' and no extension method 'value' accepting a first argument of type 'Slider' could be found (are you missing a using directive or an assembly reference?) [Assembly-CSharp]

有什么想法吗?

这是整个脚本:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class GameManager : MonoBehaviour
{

public static GameManager instance = null; //this is to create a singleton => only one instance of this script running in memory
//so we only have one Game Manager running on memory
private int activePlayer = 1; //1 = down position 2 = up position
[SerializeField]
private GameObject playerObj;
[SerializeField]
private Text timer1Text; //player1 timer text
[SerializeField]
private Text timer2Text; //player2 timer text
[SerializeField]
private GameObject startBtn;
[SerializeField]
private Texture stopTexture;
[SerializeField]
private Slider sliderObj;
private Slider sli;
public float sli_val;
private RawImage img;

private float startTime;

private bool player1Active = true;
private static float player1Time;
private static float lastP1Time;
private static float player2Time;
private static float lastP2Time;
private bool gameOver = false;
private bool gameStarted = false;
private bool gameFinished = false;

void Awake()
{
if (instance == null)
{ //check if an instance of Game Manager is created
instance = this; //if not create one
}
else if (instance != this)
{
Destroy(gameObject); //if already exists destroy the new one trying to be created
}

DontDestroyOnLoad(gameObject); //Unity function allows a game object to persist between scenes
}

public bool GameStarted
{
get { return gameStarted; }
}

public bool GameOver
{
get { return gameOver; }
}
public void StartGame()
{

if (!gameStarted)
{
gameStarted = true;
//startTime = Time.time;
//player1Time = startTime;
player1Active = true;
//select start button
img = (RawImage)startBtn.GetComponent<RawImage>();
//replace it with stop button
img.texture = (Texture)stopTexture;
}
else
{
gameStarted = false;
gameOver = true;
}

Debug.Log("StartGame");
}
public void ChangePlayer()
{
float ty = 0f;
int smileyRotate = 180;
int sliderRotate = 180;

Quaternion smileyRotation = playerObj.transform.localRotation;

ty = playerObj.transform.position.y * -1;

if (activePlayer == 1)
{
player1Active = false;
activePlayer = 2;
smileyRotate = 180;
}
else
{
player1Active = true;
activePlayer = 1;
smileyRotate = 0;
}
playerObj.transform.position = new Vector2(playerObj.transform.position.x, ty);
smileyRotation.x = smileyRotate;
playerObj.transform.localRotation = smileyRotation;
}

// Use this for initialization
void Start()
{

}

// Update is called once per frame

private string FormatTime(float pt)
{
string hours, minutes, seconds, shaon;

hours = ((int)pt / 3600).ToString("00");
minutes = ((int)pt / 60).ToString("00");
seconds = (pt % 60).ToString("00");
if ((pt / 3600) > 1)
{
shaon = string.Format("{0:D1}:{1:D2}:{2:D2}", hours, minutes, seconds);
}
else
{
shaon = string.Format("{0:D2}:{1:D2}", minutes, seconds);
}

return shaon;
}

void Update()
{

float s;
GameObject sl1 = GameObject.Find("Slider");

if (GameStarted)
{
string zman;
if (player1Active)
{
player1Time += Time.deltaTime; //if player 1 active update his time
zman = FormatTime(player1Time);
timer1Text.text = zman;
//Debug.Log("player2Time: : "+ player2Time);
}
else
{
player2Time += Time.deltaTime; //if player 2 active update his time
zman = FormatTime(player2Time);
timer2Text.text = zman;
Debug.Log("player1Time: : " + player1Time);
}

s = player1Time / (player1Time + player2Time) * 100.0f;
//sliderObj.GetComponent<Slider>;
//sliderObj.=0.5f;
sli = GameObject.Find("Slider").GetComponent<Slider>();
sli.value = s;
sliderObj.value = s;
}

}
}

最佳答案

首先感谢大家的帮助。
特别是 bpgeck 和我聊天,对我帮助很大。程序员也是正确的。我遇到的主要问题是调用附加到我的 slider Slider 的脚本的 fatal error (我仍然是一个菜鸟,但正在学习)我删除了那个脚本并创建了一个名为 Slider Percentage 的新脚本,在其中我使用更新函数来更改 slider 的值:

GameObject temp;
// Use this for initialization
void Start () {
temp = GameObject.Find("Slider");
}

// Update is called once per frame
void Update () {
temp.GetComponent<Slider>().value = GameManager.instance.GetPercentage;
}

百分比值仍在 GameManager 中计算,但使用此脚本显示。
现在一切正常!再次感谢您的帮助:-)

关于c# - 在 C# 中设置 slider 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41170088/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com