我正在嘗試學習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上。)
在此先感謝。
在使用Unity3d時,您確實想使用公共字段並避免屬性,這是遊戲將C#代碼貫穿的序列化引擎的限制。 –