C# 跨多个类的变量

C# 跨多个类的变量,c#,unity5,C#,Unity5,我试图学习C#,但在Unity中创建跨类变量时遇到了一些问题 节点类: using System.Collections; using System.Collections.Generic; using UnityEngine; [System.Serializable] public class Node { public Vector3 dimensions; public int jointType; public Vector2 jointLimit;

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

节点类:

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;
    }
}
我正在尝试创建一个节点变量和一个连接变量,这两个变量都可以被GenoManager类和BodyPart类访问(其中有很多)

车身部件等级:

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类:

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;
    }
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
[系统可序列化]
公共类管理器:MonoBehavior{
公共车身模块;
公共转换pos;
//每帧调用一次更新
无效更新(){
}
公共静态列表节点=新列表();
节点t=新节点(新向量3(1,4,1),1,向量2.up,2,0);
静态int[]testList={1,2};
公共静态连接te=新连接(Vector3.up,Vector3.up,1.5f,1,false,testList,0);
void Start(){
//节点测试生成
节点。添加(t);
打印(节点[0]);
//根块生成
BodyPart newPart=实例化(块、位置、位置旋转)为BodyPart;
newPart.transform.localScale=节点[0]。维度;
}
}
基本上,我想问的是,我该如何制作一个变量,这个变量可以在一个类中创建,并被其他类查看?(另外,如果格式设置关闭,很抱歉,这是我第一次在StackOverflow上发布。)


提前感谢。

您应该使用
公共静态变量

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

变量可以是。。。其他班级看了吗

在C#中,变量实际上是在类中定义的。你不能把它们传给别人。您可以传递具有包含值的公共属性(不使用)的类或结构

我如何制作一个可以在一个类中创建并被其他类查看的变量

理想情况下,您希望使用。也就是说,如果类A需要访问某个值(依赖项)才能在任何时间点运行,则应将其作为构造函数的一部分进行传递。理想情况下,您可以构造类B(依赖项),它将实现一个只提供其他类(类A)所需的必要属性/方法/访问的接口。例如:

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;
  }
}
在本例中,创建了
客户
,但只有通过接口所需的内容可用。调酒师现在可以访问
客户
,但无权更改
年龄
。由于
调酒师
要求创建客户,因此在创建客户时不能意外地不包括
客户

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

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


我强烈建议您不要使用静态字段。它们有很多问题(线程可能是一个严重的问题,它们往往会变得更难实现)。

使用Unity3d时,您确实希望使用公共字段并避免属性,这是游戏使C代码通过的序列化引擎的一个限制。
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();
    }
}
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;
  }
}