在 类 之间传递变量

pass variables between classes

我最近才开始接触 C#。我使用 PHP 所以我还没有进入语法。

我正在使用 Unity 并尝试在两个 class 之间发送一个变量。我有的是鼠标移动和暂停class。在 Mouse class 中,我有一个名为 state 的变量。如果为真,则脚本运行,如果为假,则不运行。

我想知道如何从另一个 class 更改此变量。由于我在这方面没有成功,所以我制作了一个名为 vars 的 class 来尝试使其工作。没有以太币。

pauseMenu.cs

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

public class pauseMenu : MonoBehaviour {
public bool isMenu = false;
public Texture btnTexture;
public bool isShown = false;
public MouseLook mouse = new MouseLook();

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



    }
void Update () {
     if (mouse.state== false) {
                    Debug.Log ("Some");
            }
            if (Input.GetKeyDown (KeyCode.P) || Input.GetKeyDown (KeyCode.Escape) && isMenu == false) {
                    Debug.Log ("Show");
                    isShown = (isShown == false) ? true : false;


            }
            if (isShown) {
                    Screen.showCursor = true;
                    Screen.lockCursor = false;
                    mouse.state= false;
            }
    }
void OnGUI()
{
    if (isShown) {

        if (GUI.Button (new Rect (10, 10, 50, 50), "Quit")) {
            Application.Quit();
            Debug.Log("Quit");
        }
    }
}
}

MouseLock.cs

using UnityEngine;
using System.Collections;

[AddComponentMenu("Camera-Control/Mouse Look")]

public class MouseLook : MonoBehaviour {

public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
public RotationAxes axes = RotationAxes.MouseXAndY;
public float sensitivityX = 15F;
public float sensitivityY = 15F;

public float minimumX = -360F;
public float maximumX = 360F;

public float minimumY = -60F;
public float maximumY = 60F;
float rotationY = 0F;
public bool state { get; set; }

void Update ()
{
            if (state == true) {
                    if (axes == RotationAxes.MouseXAndY) {
                            float rotationX = transform.localEulerAngles.y + Input.GetAxis ("Mouse X") * sensitivityX;

                            rotationY += Input.GetAxis ("Mouse Y") * sensitivityY;
                            rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);

                            transform.localEulerAngles = new Vector3 (-rotationY, rotationX, 0);
                    } else if (axes == RotationAxes.MouseX) {
                            transform.Rotate (0, Input.GetAxis ("Mouse X") * sensitivityX, 0);
                    } else {
                            rotationY += Input.GetAxis ("Mouse Y") * sensitivityY;
                            rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);

                            transform.localEulerAngles = new Vector3 (-rotationY, transform.localEulerAngles.y, 0);
                    }
            }
    if (state == false) {
        Debug.Log ("false");


    }
    }

void Start ()
{
    state = true;
    //state = true;
    Debug.Log ("Started");
    // Make the rigid body not change rotation
    if (rigidbody)
        rigidbody.freezeRotation = true;
}
}

我看到你在使用 Unity。您必须获取您试图影响的 GameObject 的实例。例如,如果您有一个附有 Hero.cs(英雄 Class)的 Hero GameObject,并且您希望 Enemy 更改生命值,则必须将 Hero GameObject 添加到 Enemy 实例中,例如:

public GameObject LocalHero;

这是通过检查器添加的 window。

现在您可以访问另一个对象中的 class。

您可以像这样在 Hero GameObject 上访问 Hero class。

LocalHero.GetComponent<Hero>.HitPoints = 20;

如果您不使用通过 UI 手动添加的实例,则必须使用类似 GameObject.Find() 或类似的东西来获取游戏对象的实例。