资源的加载
在Assets文件夹下新建Resources文件夹,然后将2D对象(Circle,Square)放进去,这时候就能内部加载Resources了。
新建C#脚本
private GameObject ob_square,ob_circle;
void Start(){
this.ob_square = Resources.load<GameObject>("Square");
this.ob_circle = Resources.load<GameObject>("Circle");
}
/*
1.Resources类只能读取Assets下名为“Resources”的文件夹里的资源。
2. Unity打包发布时,只有Resources文件夹里的资源在会被打入包中
3. Resources类加载资源时,使用”Resources”文件夹开始的相对路径,且不包含资源的扩展名
*/
/*
我们想加载Resourses文件下的某个资源,我们就可以用
Resources.Load(string path);
Resources.Load(string path, Type systemTypeInstance);
Resources.Load<T>(string path) where T : Object
*/
这时候ob_square和ob_circle就是某种资源对象了
画节点
//找到节点的坐标(x,y,0)
private float x,y;
GameObject go = Instantiate(ob_circle,new Vector3(x,y,0),Quaternion.identity);
//Quaternion.identity表示无旋转
画连线
//找到需要连线的两点坐标(x1,y1),(x2,y2) 还有旋转r_z,缩放比例s_y
private void CreateLighting(float x_1,float y_1,float x_2, float y_2)
{
arccos = (x_2 - x_1) / Mathf.Sqrt((Mathf.Pow(x_2 - x_1, 2) + Mathf.Pow(y_2 - y_1, 2)));
r_z = Mathf.Acos(arccos) * 180 / Mathf.PI - 90;
s_y = Mathf.Sqrt((Mathf.Pow(x_2 - x_1, 2) + Mathf.Pow(y_2 - y_1, 2))) - 1;
GameObject lighting = Instantiate(ob_square);
lighting.transform.position = new Vector3((x_1 + x_2) / 2, (y_1 + y_2) / 2, 0);
if (y_2 < y_1)
lighting.transform.eulerAngles = new Vector3(0, 0, -r_z);
else
lighting.transform.eulerAngles = new Vector3(0, 0, r_z);
lighting.transform.localScale = new Vector3(0.5f, s_y, 0.5f);
}