场景
-
脚本生命周期:
Reset
、Awake
、OnEnable
、Start
、FixedUpdate
(0.02s)、Update
、LateUpdate
、OnGUI
、OnDisable
、OnDestroy
-
添加爆炸力(1000大小,半径10米):
AddExplosionForce(1000,transform.position,10);
-
碰撞检测:
OnCollisionEnter(Collision collision)
OnCollisionStay
OnCollisionExit
触发检测:OnTriggerEnter(Collider other)
OnTriggerStay
OnTriggerExit
-
重力感应:
Input.acceleration
鼠标进入、停留、离开
3D物体:OnMouseEnter
、OnMouseStay
、OnMouseExit
UI:using UnityEngine.EventSystems
接口IPointerEnterHandler
-public void OnPointerEnter(PointerEventData eventData)
、IPointerExitHandler
-public void OnPointerExit(PointerEventData eventData)
滚轮滑动(望远镜):Input.GetAxis("Mouse ScrollWheel")
-
异步加载场景:(使用协程)
AsyncOperation ao;
ao = SceneManager.LoadSceneAsync("场景二");
yield return ao;
- 合并路径:
System.IO.Path.Combine
- 开始5s后调用,每2s调用一次:
InvokeRepeating("A",5,2);
- 但摄像机不可见时:
On BecameInvisible
- 读取txt
string str = Resources.Load("txt1").ToString();
用,分开一串字符:string[] words = str.Split(',');
10.删除
List<Transform> a = new List<Transform>();
void Start ()
{
foreach (Transform item in transform)
{
if (item.name.Equals("Cube"))
{
a.Add(item);
}
}
for (int i = 0; i < a.Count; i++)
{
Destroy(a[i].gameObject);
}
}
11.面板限制字段取值范围:
[Range(0,10)]
int a;
14.动态更换天空盒:RenderSettings.skybox = newSkybox;
15计时器
time_Line += Time.deltaTime;
int hour = (int)time_Line / 3600;
int minute = ((int)time_Line - hour * 3600) / 60;
int second = (int)time_Line - hour * 3600 - minute * 60;
int millisecond = (int)((time_Line - (int)time_Line) * 1000);
Tex_Linetime.text = string.Format("{0:D2}:{1:D2}:{2:D2}.{3:D3}", hour, minute, second, millisecond);
16.text = string.Format("{0:d2}:{1:d2}:{2:d2}", shi, fen, miao);
17.iTween.MoveTo(cam, iTween.Hash("x", -150, "y", 785, "z", 0, "time", 4, "delay", 0));
18.当前时间:string path = Application.dataPath + string.Format("/Photos/Photo_")+ System.DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss") + ".png";
19.划线
public void drawLine()
{
if (Input.GetMouseButton(0))
{
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
lineRenender.positionCount = index + 1;
lineRenender.SetPosition(index, hit.point));
index++;
}
}
}
20、Toggle注册
Tog_XiaDun.onValueChanged.AddListener(ison => clickTog(ison));
void clickTog(bool ison)
{
if (ison)
{
DeBug.Log(true);
}
else
{
DeBug.Log(false);
}
}
UI
- 更换Image的Sprite:
bg.sprite = Resources.Load("bg",typeof(Sprite)) as Sprite;
- 隐藏鼠标:
Cursor.visible= false;
跟换鼠标指针
public Texture2D cursora;
public CursorMode cm = CursorMode.Auto;//渲染形式,auto为平台自适应显示
Cursor.SetCursor(cursora, Vector2.zero, cm);
3.改变RectTransform大小
RectTransform.sizeDelta = new Vector2(rT.sizeDelta.x,rT.sizeDelta.y);
3.Texture2D转Sprite
img.sprite = Sprite.Create(texture,new Rect(0, 0, texture.width, texture.height), Vector2.zero);
数学
- 钳制:
a = Mathf.Clamp(a,0,MAX)
,如果a小于0,返回0;如果a大于MAX,返回MAX。 - 小数点后保留两位:
((float)Mathf.Round(Num*100))/100
stringText:.ToString("N2")
- 随机数:Unity:
UnityEngine.Random.Range(0,10)
、C#:System.Random rand =new System.Random; rand.Next(10)
。 - 四元数、欧拉角:
1. Euler To Quaternion
2.Quaternion To Euler
3.AngleAxis To Quaternion
4.Quaternion To AngleAxis - Mathf.lerp 插值运动 Mathf.MoveTowards 匀速运动
Mathf.PingPong 乒乓运动
Cube.position =new Vector3(Mathf.PingPong(Time.time * speed) , 10),0,0)
动画
- 判断动画播放结束:
AnimatorStateInfo asi = anim.GetCurrentAnimatorStateInfo (0);
if (animatorInfo.normalizedTime > 1.0f)//normalizedTime: 范围0 -- 1, 0是动作开始,1是动作结束
{
//播放结束
}
11.dotween动画连续播放:
1、Tweener tw1,Tweener tw2
Sequence se1 = DOTween.Sequence().Append(tw1).Append(tw2);
2、tw.OnComplete(() => { Fun(i); });
VR-AR
- VR项目中,射线点到UI按钮上闪烁不灵敏:是因为在VR项目中UI是在世界坐标下,按钮跟背景在同一水平面上,所以要将按钮往前移动一像素。
WEB
1.Unity发布Html网页端的调用方法
<script>
var gameInstance = UnityLoader.instantiate("gameContainer", "Build/WEB.json");
function SetUnityFun(num,num)
{
alert(num,num);
//发送消息给unity 第一个参数:挂在脚本的物体 第二个参数:unity被调用的函数 第三个参数:函数传入的参数(只能传一个参数,多个参数使用分隔符分开然后判断)
gameInstance.SendMessage("Main Camera","CubeActive",num,num);
}
</script>
存储
不同平台读取StreamingAssets下的文件路径
string filePath =
#if UNITY_ANDROID && !UNITY_EDITOR
"jar:file://" + Application.dataPath + "!/assets/" + "name";
#elif UNITY_IPHONE && !UNITY_EDITOR
Application.dataPath + "/Raw/" + "name";
#elif UNITY_STANDALONE_WIN || UNITY_EDITOR
"file://" + Application.dataPath + "/StreamingAssets" + "/" + "name";
#endif
移动端
移动端单指偏移量
Vector2 touchDeltaPosition = Input.GetTouch(0).deltaPosition;
网友评论