unity笔记(遍历子物体,改变recttransform大小,委托delegate,射线检测,游标,toggle使用)

6 unity中遍历子物体方法

 for (int i = 0; i < transform.childCount; i++)

        {

            if (transform.GetChild(i).GetComponent<Text>())

            {

                frontinit = transform.GetChild(0).GetComponent<Text>().fontSize;

            }

        }

 

7 改变recttransform大小方法

rectTransform.sizeDelta = new Vector2( yourWidth, yourHeight);

8 公共管理类中委托delegate的使用

 

a 用来申明委托事件的公用类(包括)

public class DelegateMgs

{

    #region 委托事件的申明

 

    public delegate void ChangeRotationByTween(GameObject obj, Vector3 rotationvalue, float usetime);//旋转

public static ChangeRotationByTween ChangeRotationByTweenDelegate;

 public static void ChangeRotationByTweenFuc(GameObject obj, Vector3 rotationvalue, float usetime)//旋转调用接口

    {

        if (ChangeRotationByTweenDelegate != null)

        {

            ChangeRotationByTweenDelegate(obj,rotationvalue,usetime);

        }

 }

}

b 委托功能的实现

public class DelegateEvents : MonoBehaviour {

 

void Start () {

        DelegateMgs.ChangeRotationByTweenDelegate += AchieveRotation;

        DelegateMgs.ChangePositionByTweenDelegate += AchievePosMove;

}

    void OnDisable() {

        DelegateMgs.ChangeRotationByTweenDelegate -= AchieveRotation;

        DelegateMgs.ChangePositionByTweenDelegate -= AchievePosMove;

    }

    void AchieveRotation(GameObject obj, Vector3 rotation, float time){

        obj.transform.DOLocalRotate(rotation, time);

    }

 

    void AchievePosMove(GameObject obj, Vector3 pos, float time){

        obj.transform.DOMove(pos,time);

    }

}

c 委托事件的调用

DelegateMgs.ChangePositionByTweenDelegate(GameObject.Find("RigidBodyFPSController").gameObject, GameObject.Find("PosInfos/" + transform.name + "/pos&rot").transform.position, 1);

            DelegateMgs.ChangeRotationByTweenDelegate(GameObject.Find("RigidBodyFPSController").gameObject, GameObject.Find("PosInfos/" + transform.name + "/pos&rot").transform.eulerAngles, 1);

 

9 对meshRenderer中的Materials元素动态添加、删除、修改操作

a materials元素数组转换为元素list,通过list加减元素的操作

b 把修改好的材质球list转换成数组,最后把数组赋值给renderer上的materials。

10 通过meshfiter和meshrenderer组合鼠标画线操作

(参考:https://blog.csdn.net/yufei494215506/article/details/78855965

11 通过屏幕鼠标点发射射线(射线检测)

(参考:https://blog.csdn.net/qq_36274965/article/details/79456449

 private Ray ray;

 private RaycastHit hit;

if (Input.GetMouseButton(0) && !EventSystem.current.IsPointerOverGameObject())

        {

            ray = Camera.main.ScreenPointToRay(Input.mousePosition);

            if (Physics.Raycast(ray, out hit, 100))

            {

                if (hit.transform.tag == "Roboot")

                {

                    outline.shader = changeshader;

                    shijiaoqipanel.DOLocalMoveX(initX - withValue, 1);

                }

                else

                {

                    outline.shader = Shader.Find("Standard");

                    shijiaoqipanel.DOLocalMoveX(initX, 1);

                }

            }

        }

 

12 动画制作

目标对象上添加Animation实现帧动画。

13 游标制作

a  图标的大小自适应(ugui锚点设好的、相对位置不改变的用自身的自适应)

 transform.GetComponent<RectTransform>().localScale *= Screen.width/xxxxf;

b  图标的自由移动(使用了dotween插件)

void RangeMove()

    {

        if (target.localPosition != targetPos && !ismove)

        {

            target.DOLocalMove(targetPos, (target.localPosition - targetPos).magnitude/50f);

            ismove = true;

            Debug.Log("开始移动,两点的距离:" + (target.localPosition - targetPos).magnitude);

        }

        if ((target.localPosition-targetPos).magnitude<2f && ismove)

        {

            targetPos = new Vector3(Random.Range(minx, maxx), Random.Range(miny, maxy), 0);

            ismove = false;

            Debug.Log("下个位置点的生成");

        }

    }

14 UI中image颜色渐变效果实现修改RGBA颜色通道中的Apha值

targeimg.color = new Color(targeimg.color.r, targeimg.color.g, targeimg.color.b, Mathf.Sin(sinindex%(Mathf.PI)));

利用正弦函数实现apha通道的渐变效果

15 toggle和togglegroup的使用

Toggle值变监听

 private Toggle toggleself ;变量申请

toggleself.onValueChanged.AddListener(delegate { this.SwithControl(); });监听

 public void SwithControl()被监听的功能函数

    {

        if (toggleself.isOn)

        {

            toggleLabel.color=Color.black;

            infos.SetActive(true);

            DelegateMgs.ChangePositionByTweenDelegate(GameObject.Find("RigidBodyFPSController").gameObject, GameObject.Find("PosInfos/" + transform.name + "/pos&rot").transform.position, 1);

            DelegateMgs.ChangeRotationByTweenDelegate(GameObject.Find("RigidBodyFPSController").gameObject, GameObject.Find("PosInfos/" + transform.name + "/pos&rot").transform.eulerAngles, 1);

        }

        else if (!toggleself.isOn)

        {

            toggleLabel.color=Color.white;

            infos.SetActive(false);

        }

       

}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值