Unity自定义类

在进行Unity开发时,有时我们希望能有一些便捷的函数帮助我们处理,然而Unity官方并未提供这类方法,需要我们自己写。于是本文记录一些我自己封装的方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
using UnityEngine;

public static class ExpandClass
{
public static bool AnimationAtTag(this Animator animator, string tagName, int indexLayer = 0)
{
return animator.GetCurrentAnimatorStateInfo(indexLayer).IsTag(tagName);
}

public static float GetTransformDistance(Transform player,Transform enemy)
{
return (enemy.transform.position - player.transform.position).magnitude;
}

public static Vector3 GetTransformVector3(Transform enemy,Transform player)
{
return(enemy.transform.position - player.transform.position);
}

public static void RotateTowards(Transform currentTrans,Transform targetTrans)
{
Vector3 targetVector = new Vector3(targetTrans.position.x - currentTrans.position.x, 0, targetTrans.position.z - currentTrans.position.z);
Quaternion targetQua = Quaternion.LookRotation(targetVector);
currentTrans.rotation = Quaternion.Slerp(currentTrans.rotation, targetQua, 0.9f);
}
}