跨多个变量 类

Variable across multiple classes

我正在尝试学习 C#,但在 Unity 中跨 classes 创建变量时遇到了一些问题。

节点Class:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class Node {

    public Vector3 dimensions;
    public int jointType;
    public Vector2 jointLimit;
    public int recursiveLimit;
    public int nodeId;
    public List<int> to;
    public int fro;



    public Node (Vector3 dim, int jointT, Vector2 jointL, int recLim, int id){
        dimensions = dim;
        jointType = jointT;
        jointLimit = jointL;
        recursiveLimit = recLim;
        nodeId = id;
    }
}

连接Class:

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

[System.Serializable]
public class Connection {

    public Vector3 position;
    public Vector3 orientation;
    public float scale;
    public int reflection;
    public bool terminalOnly;
    public int[] to;
    public int fro;


    public Connection (Vector3 pos, Vector3 orien, float scl, int refl, bool term, int[] to, int fro){
        position = pos;
        orientation = orien;
        scale = scl;
        reflection = refl;
        terminalOnly = term;
        this.to = to;
        this.fro = fro;
    }

}

我正在尝试使用 GenoManager class 和 BodyPart class(会有很多)都可以访问的 Node 变量和 Connection 变量。

正文部分class:

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

public class BodyPart : MonoBehaviour {

    public BodyPart block;
    public bool finished;
    //Initialization
    void Start () {
        finished = false;

    }

    //Generation of all blocks [incomplete]
    void Update () {
        while (!finished) {
            BodyPart newPart = Instantiate (block, transform.position + Vector3(0,10,0), transform.rotation) as BodyPart;
        newPart.transform.localScale = p.nodes[0].dimensions; //This line also has the error CS0119
//This line has the error CS0176
            //Trying to Scale by the dimensions (a Vector3) of the 
first Node in the nodes array



            finished = true;
        }
    }
}

GenoManager Class:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class GenoManager : MonoBehaviour {


    public BodyPart block;
    public Transform pos;


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

    }

    public static List<Node> nodes = new List<Node> ();
    Node t = new Node (new Vector3(1,4,1), 1, Vector2.up, 2, 0);
    static int[] testList = { 1, 2 };

    public static Connection te = new Connection (Vector3.up, Vector3.up, 1.5f, 1, false, testList,0); 

    void Start(){
        //Node Test Generation
        nodes.Add(t);
        print (nodes [0]);

        //Root Block Generation
        BodyPart newPart = Instantiate (block, pos.position, pos.rotation) as BodyPart;
        newPart.transform.localScale = nodes[0].dimensions;
    }
}

基本上我要问的是,我将如何制作一个可以在一个 class 中创建并被其他 class 人查看的变量? (另外,抱歉,如果格式不正确,这是我第一次在 Whosebug 上发帖。)

提前致谢。

您将使用 public static 变量 https://unity3d.com/learn/tutorials/topics/scripting/statics

using System;
public class ContainsStaticVariable
{
    public static string ExampleStaticVariable = "I am the value of a static variable";
}

public class DisplayContentsOfStaticVariable
{
    public static void Main()
    {
        Console.WriteLine(ContainsStaticVariable.ExampleStaticVariable);
        Console.WriteLine("Press return to exit...");
        Console.ReadLine();
    }
}

如果不想在运行时更改值,可以使用public const

Variable that can be ... viewed by other classes?

在 C# 中,变量实际上是在 class 中定义的。你不传递它们。您可以传递 Class 或具有 public 属性(不要使用 public fields)并保存值的结构。

how would I go about making a variable that can be created in one class and viewed by other classes?

您最好使用 Dependency inversion principle。也就是说,如果 class A 需要访问一个值(依赖项)以使其在任何时间点发挥作用,则应将其作为构造函数的一部分传递。理想情况下,您将构造 class B(依赖项),它将实现一个接口,该接口仅提供其他 classes (class A) 所需的必要 properties/methods/access。例如:

static Main()
{
  var person = new Person { Age = 21 };
  var bartender = new Bartender(person);

  if (bartender.CanServeAlcohol())
  {
  }
}

public interface ICustomer
{
  int Age { get; } 
  // Ideally this should be a DateTime with a function
  // that returns the age, so this is really just an example
}

public class Customer : ICustomer
{
  public int Age { get; set; }
}

public class Bartender
{
  public ICustomer _customer;

  public Bartender(ICustomer customer)
  {
    _customer = customer;
  }

  public bool CanServeAlcohol()
  {
    return _customer.Age >= 21;
  }
}

在此示例中,创建了 Customer,但只有通过界面需要的内容可用。 Bartender 现在可以访问 Customer,但无权更改 Age。由于 Bartender 需要创建客户,因此您不能在创建客户时不小心不包含 Customer

一旦你理解了这个原则,我强烈建议你研究一下可以自动为你做这些事情的依赖注入框架。我熟悉的流行框架是 Autofac、Unity、Ninject、StructureMap.. 等等。还有很多其他框架,它们几乎都做同样的事情。

(意识到这不是最好的例子,因为一个真正的调酒师可以服务不止一个人,但它适用于你的例子)

我强烈建议您不要使用静态字段。它们有很多问题(线程可能是一个严重的问题,它们往往会变成 god objects and make it harder to implement extensionibility)。