当前位置:网站首页>Related use of unityc intermediate grammar
Related use of unityc intermediate grammar
2022-07-16 07:10:00 【MaNongSa】
C# Intermediate grammar
Tips : The following is the main body of this article
Paradigm singleton
explain : If you don't understand the paradigm, you can jump to Official model teaching
Or jump to my C# Basic to the model description in the introductory chapter
1、 establish
public class Singleson<T> : MonoBehaviour where T :
{
private static T instance;
public static T Instance { get => instance; }
// A virtual method of the parent class
protected virtual void Awake()
{
if (instance != null)
Destroy(gameObject);
else
instance = (T)this;
}
// Judge whether it is empty / clear
protected virtual void OnDestroy()
{
if (instance == this)
{
instance = null;
}
}
// Judge whether the single instance is empty
public static bool IsInitialized
{
get { return instance != null; }
}
}
2、 Use
public class GameManager : Singleson<GameManager>
{
public int playerStats;
public void RigisterPlayer(int player)
{
playerStats = player;
}
3. summary
You don't have to create one by one Instance Single case
If you want to use singleton, inherit Singleson<T> Singleton class
}
Use of interfaces
If you don't understand the interface, you can jump to Official single case teaching
1、 establish
public interface IEndGameObsever
{
public void EndObserve();
}
2、 Use
public List<IEndGameObsever> endGameObsevers = new List<IEndGameObsever>();
public void AddObserve(IEndGameObsever observer)
{
endGameObsevers.Add(observer);
}
public void RemoveObserve(IEndGameObsever observer)
{
endGameObsevers.Remove(observer);
}
public void NotifyObserver()
{
foreach (var item in endGameObsevers)
{
item.EndObserve();
}
}
3、 effect
In a particular state If you die, you can call NotifyObserver Inform those who use this interface to do something, such as playing victory animation
public void EndObserve()
{
/// < summary >
/// The end is the interface of execution i
/// </ summary >
isWalk = false;
isChase = false;
// Perform animation of victory, etc
animator.SetBool("isVictory", true);
attackTarget = null;
isPlayerObserve = true;
}
Common and easy-to-use methods
And how to play with duohao →→→: You can jump to Official teaching
Use the code :
Create a static class
public static class ExtensionMethod
{
private const float distance = 0.5f;
public static bool isFacingTarger(this Transform transform, Transform targer)
{
Vector3 diretion = targer.transform.position - transform.position;
diretion.Normalize();
This method is to judge the right and left sides of the attack 0.5 Are there any enemies , If he runs behind the enemy before the blood deduction is triggered, he will not be attacked
var dot = Vector3.Dot(transform.forward, diretion);
return dot > distance;
}
}
explain : You can run behind the enemy when the enemy cuts down , Then you won't be attacked !
summary
Key marking text settings
Specific marking text
Specific marking text
Specific marking text
Specific marking text
Specific marking text
Specific marking text
边栏推荐
猜你喜欢
随机推荐
【C语言】 大学生考勤管理系统
Summary of some usage methods of mpu6050
Comparison and application of DHT11 and dht22 (am2302)
Openmv realizes color tracking
Drawing of iterative fractal graphics
The difference between heap and stack (articles turned countless times)
三分钟上手Markdown——基本语法快速入门
[cloud native | middleware] open source SPL can easily cope with t+0
About thread safety
Leetcode-2 addition of two numbers (head insertion, tail insertion of linked list, addition and subtraction of two linked lists with different lengths)
面试官:来说一下,你们是怎么解决分布式场景下的事务问题?
JVM原理与实战
Database dark horse notes DML
男人要“强”,不要“软”“弱”,“虚”得一匹
散列表HashTable线性探测法类模板的实现
Week4
DL第二天
LeetCode 刷题 第九天
unityC#中级语法的相关使用
Scope when multiple else if are nested








