跨多个类的变量

时间:2017-07-29 21:04:22

标签: 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;
    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;
    }
}

连接类:

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;
    }

}

我正在尝试使用一个Node变量和一个Connection变量,这些变量和一个Connection变量都可以被GenoManager类和BodyPart类(可能有很多)访问。

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;
    }
}

基本上我要问的是,我如何创建一个可以在一个类中创建并由其他类查看的变量? (另外,如果格式化已关闭,很抱歉,这是我第一次在StackOverflow上发布。)

提前致谢。

2 个答案:

答案 0 :(得分:1)

  

可以......被其他类查看的变量?

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

  

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

理想情况下,您要使用Dependency inversion principle。也就是说,如果类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;
  }
}

在此示例中,创建了Customer,但只有通过接口所需的内容才可用。调酒师现在可以访问Customer,但无法更改Age。由于Bartender要求创建客户,因此在创建客户时,您不会意外地不包含Customer

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

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

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

答案 1 :(得分:0)

您使用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

相关问题