簡體   English   中英

如何在Unity3d中使用相同的腳本為每個游戲對象設置不同的值變量

[英]How to set different value variables for each game object using same script in Unity3d

我對Unity還是很陌生,我試圖創建三個具有一個腳本的不同對象,並為每個實例設置一個變量。

我已將腳本拖放到場景中的三個對象上,並從unity ui插槽value變量設置為每個對象不同的值!

當前代碼如下:

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


public class DragBall : MonoBehaviour {

    public GameObject gameObjectToDrag;
    public Text txt;


    public int value;

    public Vector3 GOcenter;
    public Vector3 touchPosition;
    public Vector3 offset;
    public Vector3 newGOcenter;

    RaycastHit hit;

    public bool draggingMode = false;

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {
        if (Input.GetMouseButtonDown(0)) {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out hit))
            {
                gameObjectToDrag = hit.collider.gameObject;
                GOcenter = gameObjectToDrag.transform.position;
                touchPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
                offset = touchPosition - GOcenter;
                draggingMode = true;
            }
        }

        if (Input.GetMouseButton(0)) {
            if (draggingMode) {
                touchPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
                newGOcenter = touchPosition - offset;
                gameObjectToDrag.transform.position = new Vector3(newGOcenter.x, 3, newGOcenter.z); // 0 means Y axis , so let the ball go horizontally only
            }
        }

        if (Input.GetMouseButtonUp(0)) {
            draggingMode = false;
        }

        Debug.Log(this.value);
        txt.text = this.value + "";
    }
}

編輯代碼:

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


public class DragBall : MonoBehaviour {

    public GameObject gameObjectToDrag;

    public Text txt;

    public Vector3 GOcenter;
    public Vector3 touchPosition;
    public Vector3 offset;
    public Vector3 newGOcenter;

    RaycastHit hit;

    public int value;

    public bool draggingMode = false;

    // Use this for initialization
    void Start () {
        if (this.gameObjectToDrag.name == "Ball1")
        {
            this.value = 1;
        }
        else if (this.gameObjectToDrag.name == "Ball2")
        {
            this.value = 2;
        }
        else if (this.gameObjectToDrag.name == "Ball3")
        {
            this.value = 3;
        }
    }

    // Update is called once per frame
    void Update () {

        if (Input.GetMouseButtonDown(0)) {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out hit))
            {
                this.gameObjectToDrag = hit.collider.gameObject;
                GOcenter = this.gameObjectToDrag.transform.position;
                touchPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
                offset = touchPosition - GOcenter;
                draggingMode = true;
            }
        }

        if (Input.GetMouseButton(0)) {
            if (draggingMode) {
                touchPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
                newGOcenter = touchPosition - offset;

                if (this.gameObjectToDrag.name == "Ball1") {
                    this.gameObjectToDrag.transform.position = new Vector3(newGOcenter.x, 1, newGOcenter.z); // 0 means Y axis , so let the ball go horizontally only
                }
                else if (this.gameObjectToDrag.name == "Ball2")
                {
                    this.gameObjectToDrag.transform.position = new Vector3(newGOcenter.x, 2, newGOcenter.z); // 0 means Y axis , so let the ball go horizontally only
                }
                else if (this.gameObjectToDrag.name == "Ball3")
                {
                    this.gameObjectToDrag.transform.position = new Vector3(newGOcenter.x, 3, newGOcenter.z); // 0 means Y axis , so let the ball go horizontally only
                }

            }
            txt.text = this.value + "";
        }

        if (Input.GetMouseButtonUp(0)) {
            draggingMode = false;
        }
    }
}

我希望為用鼠標觸摸的每個對象顯示當前值變量。 現在,所有顯示為0。

是否應該為每個對象創建不同的腳本並將腳本分別分配給每個對象? 這對我idk來說不僅僅聽起來是正確的方法,但是必須有一些東西!

您沒有初始化value變量,因此當附加到3個Object時它變為0 即使將其初始化為數字,每個實例上仍將是該數字。

您需要一種方法來初始化每個GameObject上的value變量。 為此,您還需要一種區分每個GameObject 這可以通過比較GameObject的當前名稱,標簽,圖層或GameObject實例來完成。

下面的示例使用GameObject的名稱在Start函數中初始化value變量。 假設它們分別命名為“ Obj1”,“ Obj2”和“ Obj3”:

void Start()
{
    //Use 1 for Object 1
    if (this.gameObject.name == "Obj1")
    {
        value = 1;
    }
    //Use 3 for Object 2
    else if (this.gameObject.name == "Obj2")
    {
        value = 2;
    }
    //Use 3 for Object 3
    else if (this.gameObject.name == "Obj3")
    {
        value = 3;
    }
}

如果您在檢查器中將值設置為其他數字,則此腳本應該可以工作並在日志中顯示其他數字。 我看到的是您每次更新都運行txt.text = this.value 如果您有一個文本字段連接到球的所有實例,則它將始終顯示相同的值。 如果要讓文本字段顯示當前被拖動的球,則應將該代碼行放入if語句中,如下所示:

    if (Input.GetMouseButtonDown(0)) {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        if (Physics.Raycast(ray, out hit))
        {
            //Add line here
            txt.text = this.value + "";
            gameObjectToDrag = hit.collider.gameObject;
            GOcenter = gameObjectToDrag.transform.position;
            touchPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            offset = touchPosition - GOcenter;
            draggingMode = true;
        }
    }

如果您為每個球設置了不同的值,那么它應該起作用

在此處輸入圖片說明

暫無
暫無

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

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