用Unity2D画节点和线段

用Unity2D画节点和线段

资源的加载

在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);
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值