unity 学习笔记(二)

本文汇总了Unity游戏开发中常用的技术和技巧,包括DoTween插件的使用、协同程序的编写、物体移动与旋转控制、UGUI图片更换、事件定义与触发、AssetBundle打包与加载、角度与方向求解等,旨在帮助开发者提升Unity游戏开发效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

开始

 

DoTween插件,往前播放:DOPlayForward();

往后播放:DOPlayBackwards();

DOMoveX(5,3);   //x坐标移动到5,用时3秒

go.transform.DOBlendableScaleBy(new Vector3(0.3f, 0.3f, 0.3f), 0.4f); //在0.4秒增加0.3

相应的 go.transform.DOBlendableScaleBy(new Vector3(-0.3f, -0.3f, -0.3f), 0.4f); //在0.4秒减小0.3

 

 

协同程序:

IEnumerator One(GameObject go)     //开启一个协程,这里参数为GameObject,当go为false代表终止协程,并且当go再次开启为true时,协程也不开启

{

         Yield return new WaitForSeconds(0.4f);  //中断程序:这里暂停0.4秒

         go.SetActive(false);    

}

NetworkManager 控制联网游戏

 

左右滑动三维物体旋转角度:

public void OnMouseDrag()

    {

        this.transform.Rotate(Vector3.up, Time.deltaTime * rotateSpeed * Input.GetAxis("Mouse X"), Space.Self);  //Space.Self表示局部坐标

}

使用DoTween的transform.DoScale(new vector3(0,0,0),0.4f)实现在0.4秒的时间内,面板由原本大小缩小到坐标(0,0,0)点,

使用transform.DoScale(1f,0.4f)实现在0.4秒的时间内,面板由坐标(0,0,0)放大到原本大小

 

UGUI更改图片

  1. Image myImage;

myImage.sprite=Resources.Load(“//图片位置”+//图片名称,typeof(sprite))as Sprite;

  1. 通过协程从WWW上获取到图片

 

定义一个事件的代理

public delegate void OnPlayerInfoChangedEvent(InfoType type);

定义一个事件

public event OnPlayerInfoChangedEvent OnPlayerInfoChanged;

在别的类注册

PlayerInfo._instance.OnPlayerInfoChanged += OnPlayerInfoChanged;

在别的类销毁

PlayerInfo._instance.OnPlayerInfoChanged -= OnPlayerInfoChanged;

在定义事件的类里,写在需要改变的地方

void OnPlayerInfoChanged(InfoType type)

 

Enum类型的强转

Equipment.EquipmentType equipmentType=(Equipment.EquipmentType)System.Enum.Parse(typeof (Equipment.EquipmentType), equipmentTypeValye.ToString());

 

移动的时候改变主角的朝向

Vector3 vel = rigidbody.velocity;

        rigidbody.velocity = new Vector3(-h*velocity, vel.y, -v*velocity);

        if (Mathf.Abs(h) > 0.05 || Mathf.Abs(v) > 0.05)  //表示移动,改变主角的朝向

        {

            transform.rotation = Quaternion.LookRotation(new Vector3(-h, 0, -v));

        }

 

另一个方法:transform.LookAt(new vector(v,0,h)+transform.rotation

 

rigidbody.velocity.magnitude返回向量的长度,也就是点P(x,y,z)到原点(0,0,0)的距离。 最常用的是用来返回物体的移动速度

SetDestination(Target.position);表示寻路,目标为Target.position

 

Substring(0,4)表示从字符从第一位开始,截取4

 

在动画里面添加事件,可以使特效和动画播放的同步

 

怪物的击退效果:

transform.DOMove(transform.position + FindPlayer._instance.Player.transform.forward * 1f, 1f);

 

//将目标的世界坐标转化为屏幕坐标

 Vector2 pos = RectTransformUtility.WorldToScreenPoint(Camera.main, targetPos);

 

按钮添加事件:

Button button;

Button.onClick.AddListener(OnClick);

Void OnClcik(){

//按钮点击的操作

}

 

AssetBundle打包:

//参数:1.打包目录,2.打包选项,3.打包平台

BuildPipeline.BuildAssetBundles("AssetBundles", BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows64);

AssetBundle加载:

AssetBundle ab= AssetBundle.LoadFromFile("AssetBundles/wall.ab");//加载相对路径

 GameObject go = ab.LoadAsset<GameObject>("Wall"); //从AssetBundle取得自己的Prefab;

  Instantiate(go);    //实例化生成

 

求角度:Vector3.Angle(player1.forward,player2.forward);  //得到两者之间的角度

求方向(点积):Vector3.Dot(player1.forward,player2.forward);  //得到一个值,>0 两者同向, <0 两者反向, =0 两者垂直

求距离:方法1(plyaer1.position-player2.position).magnitude   //比较精确

方法2(player1.position-player2.position).sqrMagnitude //比较省

        方法3Vector3.Distance(player1.position,player2.position);

 

在摄像机的世界里,黑色是透明

 

RenderTexture制作动态头像

 

RectTransform.sizeDelta表示UGUI组件的尺寸

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值