2017-07-29 61 views
1

我正在嘗試學習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; 
    } 

} 

我想有一個節點的變量和連接變量都是由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; 
    } 
} 

基本上我問的是,我將如何去製作,可以在一個類中創建和查看變量其他課程? (另外,如果格式化關閉,對不起,這是我第一次發佈在StackOverflow上。)

在此先感謝。

回答

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)。

+0

在使用Unity3d時,您確實想使用公共字段並避免屬性,這是遊戲將C#代碼貫穿的序列化引擎的限制。 –

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