簡體   English   中英

使用滑塊統一移動預制件

[英]move a prefab using a slider unity

我正在使用代碼來實例化對象,當我單擊其中一個對象並更改滑塊時,它會獲取滑塊值並旋轉。現在我想沿y軸移動單擊的對象我嘗試使用相同的代碼,但它沒有我的工作錯在了什么?

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

public class dynamicmovement : MonoBehaviour
{
    public GameObject cubePrefab;
    public Slider cubeSlider;
    public Button instantiateButton;
    public Button instantiateButton2;

    public Slider cubeSlider2;


    public float speed = 0f;
    public float pos = 0f;
    public GameObject mcamera;
    Vector3 cposition;

    private Transform currentObjectToDrag = null;

    // Use this for initialization
    void Start()
    {
        //Set Slider Values
        cubeSlider.minValue = 0f;
        cubeSlider.maxValue = 360f;
        cubeSlider.value = 0f;

        cubeSlider2.minValue = 0f;
        cubeSlider2.maxValue = 360f;
        cubeSlider2.value = 0f;


    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            RaycastHit hit;
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out hit, 1000.0f))
            {
                GameObject objHit = hit.collider.gameObject;
                Debug.Log("We Clicked on : " + objHit.name);

                //Check if this is cube
                if (objHit.CompareTag("Cube"))
                {
                    Debug.Log("Cube selected. You can now drag the Cube with the Slider!");
                    //Change the current GameObject to drag
                    currentObjectToDrag = objHit.transform;
                }
            }
        }
    }

    public void instantiateCube()
    {
        //Instantiate(cubePrefab, new Vector3(0, 0, 0), Quaternion.identity);


        cposition = new Vector3(mcamera.transform.position.x + 80, mcamera.transform.position.y - 2, mcamera.transform.position.z + 10);

        cubePrefab.transform.position = cposition;
        //mcamera.transform.position = new Vector3(+10, +10, +10);


        //transform.position = new Vector3(351, -36, 60);
        Instantiate(cubePrefab, cubePrefab.transform.position, transform.rotation);






       //Instantiate(cubePrefab, new Vector3(519f, -41f, 170f), Quaternion.identity);
    }

    public void rotatess(float newspeed)
    {
        speed = newspeed;

    }

    public void positions(float newpos)
    {
        pos = newpos;
    }

    //Called when Instantiate Button is clicked
    void instantiateButtonCallBack()
    {
        Debug.Log("Instantiate Button Clicked!");
        instantiateCube();
    }

    //Called when Slider value changes
    void sliderCallBack(float value)
    {
        Debug.Log("Slider Value Moved : " + value);

        //Move the Selected GameObject in the Z axis with value from Slider
        if (currentObjectToDrag != null)
        {
           // currentObjectToDrag.position = new Vector3(519f, -41, value);
            currentObjectToDrag.eulerAngles = new Vector3(0, value, 0);
            Debug.Log("Position changed!");
        }
    }

    void sliderCallBack2(float value)
    {
        Debug.Log("Slider Value Moved : " + value);

        //Move the Selected GameObject in the Z axis with value from Slider
        if (currentObjectToDrag != null)
        {
            // currentObjectToDrag.position = new Vector3(519f, -41, value);
            currentObjectToDrag.position = new Vector3(0, value, 0);
            Debug.Log("Position changed!");
        }
    }

    //Subscribe to Button and Slider events
    void OnEnable()
    {
        instantiateButton.onClick.AddListener(instantiateButtonCallBack);
        cubeSlider.onValueChanged.AddListener(delegate { sliderCallBack(cubeSlider.value); });
    }

    //Un-Subscribe to Button and Slider events
    void OnDisable()
    {
        instantiateButton.onClick.RemoveListener(instantiateButtonCallBack);
        cubeSlider.onValueChanged.RemoveListener(delegate { sliderCallBack(cubeSlider.value); });
    }

    void OnEnable()
    {
        instantiateButton.onClick.AddListener(instantiateButtonCallBack);
        cubeSlider2.onValueChanged.AddListener(delegate { sliderCallBack2(cubeSlider2.value); });
    }

    //Un-Subscribe to Button and Slider events
    void OnDisable()
    {
        instantiateButton.onClick.RemoveListener(instantiateButtonCallBack);
        cubeSlider2.onValueChanged.RemoveListener(delegate { sliderCallBack2(cubeSlider2.value); });
    }
}

您不能定義兩次函數( OnEnableOnDisable )。 您可以在一個OnEnableOnDisable函數中注冊cubeSlidercubeSlider事件。

public class dynamicmovement : MonoBehaviour
{

    public GameObject cubePrefab;
    public Slider cubeSlider;
    public Button instantiateButton;
    public Button instantiateButton2;

    public Slider cubeSlider2;


    public float speed = 0f;
    public float pos = 0f;
    public GameObject mcamera;
    Vector3 cposition;

    private Transform currentObjectToDrag = null;

    // Use this for initialization
    void Start()
    {
        //Set Slider Values
        cubeSlider.minValue = 0f;
        cubeSlider.maxValue = 360f;
        cubeSlider.value = 0f;

        cubeSlider2.minValue = 0f;
        cubeSlider2.maxValue = 360f;
        cubeSlider2.value = 0f;


    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            RaycastHit hit;
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out hit, 1000.0f))
            {
                GameObject objHit = hit.collider.gameObject;
                Debug.Log("We Clicked on : " + objHit.name);

                //Check if this is cube
                if (objHit.CompareTag("Cube"))
                {
                    Debug.Log("Cube selected. You can now drag the Cube with the Slider!");
                    //Change the current GameObject to drag
                    currentObjectToDrag = objHit.transform;
                }
            }
        }
    }

    public void instantiateCube()
    {
        //Instantiate(cubePrefab, new Vector3(0, 0, 0), Quaternion.identity);


        cposition = new Vector3(mcamera.transform.position.x + 80, mcamera.transform.position.y - 2, mcamera.transform.position.z + 10);

        cubePrefab.transform.position = cposition;
        //mcamera.transform.position = new Vector3(+10, +10, +10);


        //transform.position = new Vector3(351, -36, 60);
        Instantiate(cubePrefab, cubePrefab.transform.position, transform.rotation);






        //Instantiate(cubePrefab, new Vector3(519f, -41f, 170f), Quaternion.identity);
    }

    public void rotatess(float newspeed)
    {
        speed = newspeed;

    }

    public void positions(float newpos)
    {
        pos = newpos;
    }

    //Called when Instantiate Button is clicked
    void instantiateButtonCallBack()
    {
        Debug.Log("Instantiate Button Clicked!");
        instantiateCube();
    }

    //Called when Slider value changes
    void sliderCallBack(float value)
    {
        Debug.Log("Slider Value Moved : " + value);

        //Move the Selected GameObject in the Z axis with value from Slider
        if (currentObjectToDrag != null)
        {
            // currentObjectToDrag.position = new Vector3(519f, -41, value);
            currentObjectToDrag.eulerAngles = new Vector3(0, value, 0);
            Debug.Log("Position changed!");
        }
    }

    void sliderCallBack2(float value)
    {
        Debug.Log("Slider Value Moved : " + value);

        //Move the Selected GameObject in the Z axis with value from Slider
        if (currentObjectToDrag != null)
        {
            // currentObjectToDrag.position = new Vector3(519f, -41, value);
            currentObjectToDrag.position = new Vector3(0, value, 0);
            Debug.Log("Position changed!");
        }
    }

    //Subscribe to Button and Slider events
    void OnEnable()
    {
        instantiateButton.onClick.AddListener(instantiateButtonCallBack);
        cubeSlider.onValueChanged.AddListener(delegate { sliderCallBack(cubeSlider.value); });
        cubeSlider2.onValueChanged.AddListener(delegate { sliderCallBack2(cubeSlider2.value); });
    }

    //Un-Subscribe to Button and Slider events
    void OnDisable()
    {
        instantiateButton.onClick.RemoveListener(instantiateButtonCallBack);
        cubeSlider.onValueChanged.RemoveListener(delegate { sliderCallBack(cubeSlider.value); });
        cubeSlider2.onValueChanged.RemoveListener(delegate { sliderCallBack2(cubeSlider2.value); });
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM