using System.Collections; using System.Collections.Generic; using UnityEngine; //SceneManager的命名空间 using UnityEngine.SceneManagement; public class Manager : MonoBehaviour { void Start () { StartCoroutine (LoadScene ()); } IEnumerator LoadScene(){ SceneManager.LoadScene ("12", LoadSceneMode.Additive); yield return new WaitForSeconds(5f); SceneManager.UnloadSceneAsync ("12"); yield return new WaitForSeconds (1f); //加载下一个场景 SceneManager.LoadScene("20",LoadSceneMode.Additive); } }
可以认为这个就是管理场景的一个脚本,而这个场景就是那个将会一直存在的场景
通过这个场景的管理类,额外的添加其他的场景,也可以在不需要时卸载,再添加其他场景
从而实现了一种不用DontDestroyOnLoaded的更好办法,场景特么也成为一种组件的形式了
物体需要带有Collider组件,2D的类似
void Start () { Collider[] colliders = Physics.OverlapSphere (transform.position, 3); foreach (Collider c in colliders) { if (c.gameObject != this.gameObject) { Debug.Log (c.gameObject.name); } } }
Physics.OverlapSphere(中心,半径)表示检测在中心点和半径组成的圆内的物体们
2D类似,Sphere球形,还有其他的形状,自行查看API
overlap n.重叠
[Serializable] public class UIPanelInfo : ISerializationCallbackReceiver{ public UIPanelType panelType; public string panelTypeString; public string path; public void OnBeforeSerialize(){ //序列化前调用 } public void OnAfterDeserialize(){ //反序列化之后调用 UIPanelType type = (UIPanelType) System.Enum.Parse (typeof(UIPanelType), panelTypeString); panelType = type; } }
实现接口ISerializationCallbackReceiver,与两个方法
OnBeforeSerialize() 序列化之前调用,可以将要序列化的数据进一步处理后再序列化,如加密
OnAfterDeserialize() 反序列化之后调用,可以将反序列化的数据(对象的数据)进一步处理,如解密
使用JsonUtility解析json时,需要提供一个允许序列化的对象类,对应的对上json的数据名称和数据类型。
[Serializable] public class UIPanelInfo { public string panelTypeString; public string path; } //对应json {"panelTypeString":"systemPanel","path":"Panel/systemPanel"} ----------------------------------------------------------------- [Serializable] public class UIPanelJson { public List<UIPanelInfo> uiPanelInfoList; } //对应json { "uiPanelInfoList":[ {"panelTypeString":"bagPanel","path":"Panel/bagPanel"} {"panelTypeString":"systemPanel","path":"Panel/systemPanel"} ] }
[Serializable]表名类是可以序列化的,此特性在System命名空间里
使用JsonUtility解析json文件时,注意json文件要以对象开头
也就是说,json要第一层要以{}包裹也就是对象的形式,不能用[]开头
{ "uiPanelInfoList":[ {"panelTypeString":"bagPanel","path":"Panel/bagPanel"}, {"panelTypeString":"shopPanel","path":"Panel/shopPanel"}, {"panelTypeString":"systemPanel","path":"Panel/systemPanel"} ] }
Vector3.Lerp( Vector3.Lerp(P0,P1,t), Vector3.Lerp(P1,P2,t) ,t ); B(t) = (1-t)((1-t)P0 + tP1) + t((1-t)P1 + tP2); //化简 B(t) = (1-t)²P0 + 2(1-t)tP1 + t²P2; //求导,获取切线加速度 B′(t) = 2(1-t)(P1-P0) + 2t(P2-P1);
这样子可以用B(t)公式替代线性插值。
Vector3.Lerp(p0,p1,t); //线性插值 B(t) = (1-t)P0 + tP1 = P0 + t(p1-p0);
一阶的贝塞尔曲线其实就是线性的一段插值
void Reset(){ }
这个方法当组件创建或点击Inspect的重置按钮时调用,不过只在编辑模式下有用。
一般用来给一些值提供常用的默认值。
先在Hierarchy中制作组合好预设需要的样子,然后拖拽这个GameObject到Project文件位置,这样,预设就生成了,同时Hierarchy的原物体名字变成蓝色。
public class Ball:MonoBehaviour{ void OnBecameInvisible(){ Destroy(this.gameObject); } }
OnBecameInvisible函数会在游戏对象移动到画面之外时调用的方法。