在C#中设置滑块值

时间:2016-12-15 17:31:43

标签: c# unity3d

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

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

滑块不是Interactable(因此我可以显示该值,但用户无法更改它)。

我定义了一个变量:

[SerializeField] private Slider sliderObj;     

并将我的滑块放在此字段中的检查器中 我有一个浮动s变量,我想在滑块中反映出来:

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

但是当我写道:

sliderObj.value = s;

我看到了这个错误:

  

'滑块'不包含'值'的定义没有延伸方法'价值'接受第一个类型为' Slider'可以找到(你错过了使用指令或程序集引用吗?)[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;
        }

    }
}

1 个答案:

答案 0 :(得分:2)

首先,感谢您的帮助 特别是bpgeck与我聊天,并帮助了我很多。 程序员也是对的。 我遇到的主要问题是调用附加到我的滑块Slider的脚本的致命错误(我仍然是一个菜鸟,但学习) 我删除了那个脚本并创建了一个名为Slider Percentage的新脚本,在其中我使用update函数来更改滑块的值:

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中计算,但使用此脚本显示 现在一切都有效!!再次感谢您的帮助: - )