unity3D 功能脚本备忘【1】(方法)

原创文章如需转载请注明:转载自 脱莫柔Unity3D学习之旅 QQ群:【Unity3D(AR/VR) 334163814】【Unity3D(游戏) 119706192】 本文链接地址: 功能脚本备忘【1】(方法)

--------------------------------------------------------------割---------------------------------------------------------------------

锁定目标帧率

1.QualitySettings界面的VSync count属性选择Don't Sync。
2.代码:
Application.targetFrameRate = 30;//手游30帧

系统休眠

Screen.sleepTimeout = (int)SleepTimeout.NeverSleep;//从不休眠
//(int)SleepTimeout.SystemSetting //按照系统设置。

延迟执行某方法

void Start () {  
	//重复调用
	InvokeRepeating("LaunchProjectile", 1,5);//1秒后调用LaunchProjectile () 函数,之后每5秒调用一次  

	//调用一次
	Invoke("LaunchProjectile", 5);//5秒后调用LaunchProjectile () 函数

	//取消调用
	CancelInvoke("LaunchProjectile"); 
}  

void LaunchProjectile () {  
	print("hello");  
}
	

协同程序

同步执行

//运行结果:"Starting 0.0"和"Before WaitAndPrint Finishes 0.0"两句,2秒后打印"WaitAndPrint 2.0"
//WaitAndPrint在Start函数内执行,可以视同于它与Start函数同步执行.
void Start() {
	print("Starting " + Time.time);
	StartCoroutine(WaitAndPrint(2.0F));
	print("Before WaitAndPrint Finishes " + Time.time);
}
IEnumerator WaitAndPrint(float waitTime) {
	yield return new WaitForSeconds(waitTime);
	print("WaitAndPrint " + Time.time);
}

执行完成后,执行下面的

//运行结果:0秒时打印"Starting 0.0",2秒后打印"WaitAndPrint 2.0"和"Done 2.0"
// 运行WaitAndPrint直到完成。
IEnumerator Start() {
	print("Starting " + Time.time);
	yield return StartCoroutine(WaitAndPrint(2.0F));
	print("Done " + ime.time);
}
IEnumerator WaitAndPrint(float waitTime) {
	yield return new WaitForSeconds(waitTime);
	print("WaitAndPrint " + Time.time);
}
	

同步执行

//StopCoroutine停止协同程序
IEnumerator Start() {
	StartCoroutine("DoSomething", 2.0F);
	yield return new WaitForSeconds(1);
	StopCoroutine("DoSomething");
}
IEnumerator DoSomething(float someParameter) {
	while (true) {
		print("DoSomething Loop");
		yield return null;
	}
}

子/父级关系

设置子/父级关系

//B为A的父级
A.transform.parent = B.transform;

获取所有子物体

foreach (Transform item in this.transform)
{
	print(item.name);
}

旋转/角度

旋转(手机)屏幕

//Portrait          正常(头朝上)
//LandscapeLeft     头朝左
//LandscapeRight    头朝右
Screen.orientation = ScreenOrientation.LandscapeLeft;

限制旋转角度

/// <summary>
/// 限制旋转角度 
/// </summary>
/// <param name="angle" />要旋转的Deg
/// <param name="min" />最小角度
/// <param name="max" />最大角度
/// <returns>符合标准的deg</returns>
public static float ClampAngle(float angle, float min, float max)
{
    if (angle < -360)
        angle += 360;
    if (angle > 360)
        angle -= 360;
    return Mathf.Clamp(angle, min, max);
}

限制旋转最大扭距

/// <summary>
/// 限制旋转最大扭距 
/// </summary>
/// <param name="angleTarget">要达到的deg
/// <param name="angleNow">当前角度
/// <returns>符合标准的deg</returns>
public static float reviseMaxAngle(float angleTarget, float angleNow)
{
    float max = 160;

    float DValue = angleTarget - angleNow;

    if (DValue < -360)
        DValue += 360;
    if (DValue > 360)
        DValue -= 360;

    if (DValue > max)
    {
        DValue = max;
    }
    else if (DValue < -max)
    {
        DValue = -max;
    }
    angleTarget = angleNow + DValue;

    return angleTarget;
}

Edit

DrawGizmos

void OnDrawGizmos()
{
    Gizmos.color = Color.red;
    Gizmos.DrawCube(transform.position,new Vector3(1,1,2));
}

Other

确定目标可见性(α透明)

bool isVisible = (gameCamera.isOrthoGraphic || pos.z > 0f) && (!disableIfInvisible || (pos.x > 0f && pos.x < 1f && pos.y > 0f && pos.y < 1f));
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值