教程:https://unity3d.com/cn/learn/tutorials/s/scripting

补充:http://www.runoob.com/csharp/csharp-inheritance.html

C#复习结合Unity3D复习笔记,用时一天。

  1. syntax 语法
  2.  
  3. perpendicular 垂直
  4.  
  5. parenthesis 圆括号
  6.  
  7. pivot 旋转轴
  8.  
  9. Ternary 三元的
  10.  
  11. generic 通用类
  12.  
  13. Polymorphism 多态

概念

Beginner Gameplay Scripting


  1. Scripts as Behaviour Components
  2. Variables and Functions
  3. Conventions and Syntax
  4. C# vs JS syntax
  5. IF Statements
  6. Loops
  7. Scope and Access Modifiers
  8. Awake and Start
  9. Update and FixedUpdate
  10. Vector Maths
  11. Enabling and Disabling Components
  12. Activating GameObjects
  13. Translate and Rotate
  14. Look At
  15. Linear Interpolation
  16. Destroy
  17. GetButton and GetKey
  18. GetAxis
  19. OnMouseDown
  20. GetComponent
  21. Delta Time
  22. Data Types
  23. Classes
  24. Instantiate
  25. Arrays
  26. Invoke
  27. Enumerations
  28. Switch Statements

List

01.

  1. public class NewBehaviourScript : MonoBehaviour
  2. {
  3. void Update()
  4. {
  5. if (Input.GetKeyDown(KeyCode.R))
  6. {
  7. GetComponent<Renderer>().material.color = Color.red;
  8. }
  9.     ...
  10. }
  11. }

05.

  1. ranslation = Time.deltaTime

参见:Unity&C# Time时间相关

07.

C#中五种访问修饰符作用范围 public、private、protected、internal、protected internal

08.

Awake()可用于object的初始化。

  1. public class AwakeAndStart : MonoBehaviour
  2. {
  3. void Awake ()
  4. {
  5. Debug.Log("Awake called.");
  6. }
  7.  
  8. void Start ()
  9. {
  10. Debug.Log("Start called.");
  11. }
  12. }

09.

Unity3D--Update和FixedUpdate的区别与共性

10.

向量计算再议。

11, 12.

  1. myLight.enabled = True;
  2.  
  3. GameObject.SetActive(false);

13.

  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class TransformFunctions : MonoBehaviour
  5. {
  6. public float moveSpeed = 10f;
  7. public float turnSpeed = 50f;
  8.  
  9. void Update ()
  10. {
  11. if(Input.GetKey(KeyCode.UpArrow))
  12. transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);  // Or 采用fixedupate
  13.  
  14. if(Input.GetKey(KeyCode.DownArrow))
  15. transform.Translate(-Vector3.forward * moveSpeed * Time.deltaTime);
  16.  
  17. if(Input.GetKey(KeyCode.LeftArrow))
  18. transform.Rotate(Vector3.up, -turnSpeed * Time.deltaTime);
  19.  
  20. if(Input.GetKey(KeyCode.RightArrow))
  21. transform.Rotate(Vector3.up, turnSpeed * Time.deltaTime);
  22. }
  23. }

14.

  1. // 摄像头貌似没动
    Transform target;
  2. transform.LookAt(target);

15.

插值的典型应用,光线的逐渐变化。

  1. /*
  2. * 根据比例找到中间的一个点
  3. */
  4. float result = Mathf.Lerp (3f, 5f, 0.5f);
  5.  
  6. Vector3 from = new Vector3 (1f, 2f, 3f);
  7. Vector3 to = new Vector3 (5f, 6f, 7f);
  8. // Here result = (4, 5, 6)
  9. Vector3 result = Vector3.Lerp (from, to, 0.75f);
  10.  
  11. /*
  12. * 变化比例,从而移动这个中间的点
  13. */
  14. light.intensity = Mathf.Lerp(light.intensity, 8f, 0.5f);
  15.  
  16. light.intensity = Mathf.Lerp(light.intensity, 8f, 0.5f * Time.deltaTime);

16.

  1. Destroy(gameObject); // destroy object
  2. Destroy(GetComponent<MeshRenderer>()); // destroy component

17.

  1. bool down = Input.GetButtonDown("Jump");
  2. bool held = Input.GetButton("Jump");
  3. bool up = Input.GetButtonUp("Jump");
 

准备

按下

按完

按完

结束

GetButtonDown

false

true

false

false

false

GetButton

false

true

true

false

false

GetButtonUp

false

false

false

true

false

18.

看样子是input的全局性的设置,待日后理解。

  1. public class AxisExample : MonoBehaviour
  2. {
  3. public float range;
  4. public GUIText textOutput;
  5.  
  6. void Update ()
  7. {
  8. float h = Input.GetAxis("Horizontal");
  9. float xPos = h * range;
  10.  
  11. transform.position = new Vector3(xPos, 2f, );
  12. textOutput.text = "Value Returned: "+h.ToString("F2");
  13. }
  14. }

19.

鼠标事件,以及在gravity模式下的“加力打击”效果。

  1. public class MouseClick : MonoBehaviour
  2. {
  3. void OnMouseDown ()  // 当object收到鼠标点击事件时
  4. {
  5. rigidbody.AddForce(-transform.forward * 500f);  //受力方向
  6. rigidbody.useGravity = true;
  7. }
  8. }
  1. OnMouseDown (脚本参考)
  2. OnMouseDrag (脚本参考)
  3. OnMouseEnter (脚本参考)
  4. OnMouseExit (脚本参考)
  5. OnMouseOver (脚本参考)
  6. OnMouseUp (脚本参考)
  7. OnMouseUpAsButton (脚本参考)

其它点击事件

24.

  1. public class UsingInstantiate : MonoBehaviour
  2. {
  3. public Rigidbody rocketPrefab;
  4. public Transform barrelEnd;
  5.  
  6. void Update ()
  7. {
  8. if(Input.GetButtonDown("Fire1"))
  9. {
  10. Rigidbody rocketInstance;
  11. rocketInstance = Instantiate(rocketPrefab, barrelEnd.position, barrelEnd.rotation) as Rigidbody;
  12. rocketInstance.AddForce(barrelEnd.forward * );
  13. }
  14. }
  15. }

子弹在一定时间后可以销毁掉。

  1. public class RocketDestruction : MonoBehaviour
  2. {
  3. void Start()
  4. {
  5. Destroy (gameObject, 1.5f);
  6. }
  7. }

25.

  1. public class Arrays : MonoBehaviour
  2. {
  3. public GameObject[] players;
      //pubiic int[] myIntArray = new int[5];
  4.  
  5. void Start ()
  6. {
  7. players = GameObject.FindGameObjectsWithTag("Player");
  1. for(int i = ; i < players.Length; i++)
  2. {
  3. Debug.Log("Player Number "+i+" is named "+players[i].name);
  4. }
  5. }
  6. }

27.

  1. public class EnumScript : MonoBehaviour
  2. {
  3. enum Direction {North, East, South, West};
  4. dir = Direction.South;
  5. ...
  6. }

28.

  1. switch (intelligence)
  2. {
  3.   case :
  4.     print ("Why hello there good sir! Let me teach you about Trigonometry!");
  5.     break;
  6.   ...
  7.   default:
  8.     print ("Incorrect intelligence level.");
  9.     break;
  10. }

Intermediate Gameplay Scripting


  1. Properties
  2. Ternary Operator
  3. Statics
  4. Method Overloading
  5. Generics
  6. Inheritance
  7. Polymorphism
  8. Member Hiding
  9. Overriding
  10. Interfaces
  11. Extension Methods
  12. Namespaces
  13. Lists and Dictionaries
  14. Coroutines
  15. Quaternions
  16. Delegates
  17. Attributes
  18. Events

List

1.

属性:可以理解为针对某一个变量的小函数。

  1. public class Player
  2. {
  3. private int experience;
  4.  
  5. public int Experience
  6. {
  7. get
  8. {
  9. //Some other code
  10. return experience;
  11. }
  12. set
  13. {
  14. //Some other code
  15. experience = value;
  16. }
  17. }
  18.  
  19. public int Level
  20. {
  21. get
  22. {
  23. return experience / ;
  24. }
  25. set
  26. {
  27. experience = value * ;
  28. }
  29. }
  30. }

使用环境:

  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class Game : MonoBehaviour
  5. {
  6. void Start ()
  7. {
  8. Player myPlayer = new Player();
  9.  
  10. //Properties can be used just like variables
  11. myPlayer.Experience = ;
  12. int x = myPlayer.Experience;
  13. }
  14. }

2.

??运算符,后面跟着的是一个“保险值”。

  1. class NullCoalesce
  2. {
  3. static int? GetNullableInt()
  4. {
  5. return null;
  6. }
  7.  
  8. static string GetStringValue()
  9. {
  10. return null;
  11. }
  12.  
  13. static void Main()
  14. {
  15. int? x = null;
  16.  
  17. // Set y to the value of x if x is NOT null; otherwise,
  18. // if x == null, set y to -1.
  19. int y = x ?? -;
  20.  
  21. // Assign i to return value of the method if the method's result
  22. // is NOT null; otherwise, if the result is null, set i to the
  23. // default value of int.
  24. int i = GetNullableInt() ?? default(int);
  25.  
  26. string s = GetStringValue();
  27. // Display the value of s if s is NOT null; otherwise,
  28. // display the string "Unspecified".
  29. Console.WriteLine(s ?? "Unspecified");
  30. }
  31. }

5.

设计一个通用方法:

  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class SomeClass
  5. {
  6. public T GenericMethod<T>(T param)
  7. {
  8. return param;
  9. }
  10. }
  1. SomeClass myClass = new SomeClass();
  2. myClass.GenericMethod<int>();

设计一个通用类:

  1. public class GenericClass <T>
  2. {
  3. T item;
  4.  
  5. public void UpdateItem(T newItem)
  6. {
  7. item = newItem;
  8. }
  9. }
  1. GenericClass<int> myClass = new GenericClass<int>();
  2. myClass.UpdateItem();

6, 7

  1. public class Apple : Fruit
  2. {
  3. public Apple():base()   // 调用基类的构造方法
  4. {
  5. color = "red";
  6. Debug.Log("1st Apple Constructor Called");
  7. }
    }
  1. base.GetInfo();        //调用基类的方法,显示Andy.

本部分介绍以下访问关键字:

  • base 访问基类成员。

  • this 引用类的当前实例。

参考:C# base和this的用法

-- 代码执行 --

  1. MyDerivedClass myder = new MyDerivedClass();
  2. MyDerivedClass myder2 = new MyDerivedClass();MyDerivedClass myder3 = new MyDerivedClass(, );

-- 构造函数执行顺序 --

100                                       // 1,然后因为2中的this(54), 所以将要调用3【注意,这个子类的构造函数是个static】
Invoke parent class with one param, i=54  // 因为base(i),调用父类
MyDerivedClass(int i)                      // 3
54                                         // 3

102                                        // 3

101                                        // this(54)结束,返回2

Invoke parent class with one param, i=5    // 调用3,调用base(5)
MyDerivedClass(int i)                      // 3
5                                          // 3

102                                        // 3

Invoke parent class without param          // 没有写base,所以调用默认的父类构造函数
Two param. i=5, j=6                        // 4

子类:

  1. namespace ConsoleApplication1
  2. {
  3. public class MyDerivedClass : MyBaseClass
  4. {
  5. public int age;
  6. public static int age2;
  7. static MyDerivedClass()                // 1
  8. {
  9. age2 = ;
  10. Console.WriteLine(age2);
  11. }
  12.  
  13. public MyDerivedClass(): this()          // 2
  14. {
  15. age = ;
  16. Console.WriteLine(age);
  17. }
  18.  
  19. public MyDerivedClass(int i): base(i)        // 3
  20. {
  21. age = ;
  22. Console.WriteLine("MyDerivedClass(int i)");
  23. Console.WriteLine(i);
  24. Console.WriteLine(age);
  25. }
  26.  
  27. public MyDerivedClass(int i, int j)         // 4
  28. {
  29. Console.WriteLine("Two param. i={0:D}, j={1:D}", i, j);
  30. }
  31. }
  32. }

Polymorphism,多态

  1. Fruit myFruit = new Apple();
  2. Apple myApple = (Apple)myFruit;

8, 9

  1. static void Main(string[] args)
  2. {
  3.   B b = new B();
  4.   b.ClassA();
  5.   A a = b;  // -->a
  6.   a.ClassA();
  7.  
  8.   Console.WriteLine("\n");
  9.  
  10.   B2 b2 = new B2();
  11.   b2.ClassA2();
  12.   A2 a2 = b2;  // -->b
  13.   a2.ClassA2();
  14.  
  15.   Console.ReadKey();
  16. }

-->a:

  1. class B : A
  2. {
  3.   new public void ClassA()
  4.   {
  5.     Console.WriteLine("B.ClassA()");
  6.   }
  7. }

-->b:

  1. A中方法用virtual
  2. B中对应方法override

10.

通过接口实现多继承。

  1. public interface IKillable
  2. {
  3. void Kill();
  4. }
  5.  
  6. public interface IDamageable<T>
  7. {
  8. void Damage(T damageTaken);
  9. }
  1. public class Avatar : MonoBehaviour, IKillable, IDamageable<float>
  2. {
  3. //The required method of the IKillable interface
  4. public void Kill()
  5. {
  6. //Do something fun
  7. }
  8.  
  9. //The required method of the IDamageable interface
  10. public void Damage(float damageTaken)
  11. {
  12. //Do something fun
  13. }
  14. }

11.

  1. //It is common to create a class to contain all of your
  2. //extension methods. This class must be static.
  3. public static class ExtensionMethods
  4. {
  5. //Even though they are used like normal methods, extension
  6. //methods must be declared static. Notice that the first
  7. //parameter has the 'this' keyword followed by a Transform
  8. //variable. This variable denotes which class the extension
  9. //method becomes a part of.
  10. public static void ResetTransformation(this Transform trans)
  11. {
  12. trans.position = Vector3.zero;
  13. trans.localRotation = Quaternion.identity;
  14. trans.localScale = new Vector3(, , );
  15. }
  16. }
  1. public class SomeClass : MonoBehaviour
  2. {
  3. void Start () {
  4. //Notice how you pass no parameter into this
  5. //extension method even though you had one in the
  6. //method declaration. The transform object that
  7. //this method is called from automatically gets
  8. //passed in as the first parameter.
  9. transform.ResetTransformation();
  10. }
  11. }

参考:C#中的扩展方法学习总结

过去的思路是:我们首先需要获得某个类的源代码,然后在这个类代码中增加成员方法,这样就可以达到为一个类提供扩展方法的目的。

可是不幸地是,这种方法在没有源代码的情况下就无法奏效了,而且我们人为地去改变源代码有可能会破坏整个代码的稳定性。

那么有没有一种方法能在不改变源代码的前提下为某个类提供扩展方法呢?这就是扩展方法。

第一个参数必须是使用this关键字指明要实现扩展方法的类。

例如:

  1. public static void SetPositionZ(this Transform tran, float z)
  2. {
  3. tran.position = new Vector3(tran.position.x, tran.position.y, z);
  4. }
  5.  
  6. transform.SetPositionZ(1.0f); // <-- 如此用就方便些
  7.  
  8. // 属性就是函数名,真正的参数就是第二个参数

12.

  1. using io = System.IO;   // 给命名空间 System.IO 定义了一个别名,叫io
  2. io.File.Create();

除了防止不同开发人员类名命名重复外,而且:

超出{}范围外,using后的对象就被释放,我们就不需要再写connction.close()了。

有点Python中的with...as的意思。

  1. //超出{}范围外,using后的对象就被释放
  2.  
  3. using (SqlConnection connection = new SqlConnection(connectionString))
  4. {
  5.   connection.open();
  6. }

参考:C# 命名空间(Namespace)

13.

列表:

  1. using System.Collections.Generic;
  2.  
  3. public class SomeClass : MonoBehaviour
  4. {
  5. void Start ()
  6. {
  7. List<BadGuy> badguys = new List<BadGuy>();
  8.  
  9. badguys.Add( new BadGuy("Harvey", ));
  10.  
  11. badguys.Sort();
  12.  
  13. foreach(BadGuy guy in badguys)
  14. {
  15. print (guy.name + " " + guy.power);
  16. }
  17.  
  18. badguys.Clear();
  19. }
  20. }

字典:

  1. using System.Collections.Generic;
  2.  
  3. public class SomeOtherClass : MonoBehaviour
  4. {
  5. void Start ()
  6. {
  7. Dictionary<string, BadGuy> badguys = new Dictionary<string, BadGuy>();
  8.  
  9. BadGuy bg1 = new BadGuy("Harvey", );
  10. badguys.Add("gangster", bg1);
  11.  
  12. BadGuy magneto = badguys["mutant"]; //提取//This is a safer, but slow, method of accessing
  13. //values in a dictionary.
  14. if(badguys.TryGetValue("birds", out temp))
  15. {
  16. //success!
  17. }
  18. else
  19. {
  20. //failure!
  21. }
  22. }
  23. }

补充:

尽管 ref 和 out 在运行时的处理方式不同,但在编译时的处理方式相同。

因此,如果一个方法采用 ref 参数,而另一个方法采用 out 参数,则无法重载这两个方法。

例如,从编译的角度来看,以下代码中的两个方法是完全相同的,因此将不会编译以下代码:

  1. class CS0663_Example
  2. {
  3. public void SampleMethod(ref int i) { }  // <---- 必须在传递之前进行初始化
  4. public void SampleMethod(out int i) { }
  5. }

参考:C# ref与out关键字解析

ref和out最主要的区别是:

  • ref将参数的参数值和引用都传入方法中,所以ref的参数的初始化必须在方法外部,进行,也就是ref的参数必须有初始化值,否则程序会报错;【有进有出】
  • out不会将参数的参数值传入方法中,只会将参数的引用传入方法中,所以参数的初始化工作必须在其对用方法中进行,否则程序会报错;【只出没进】

14.

协程:Coroutines

参考:全面解析Coroutine技术

.Net为了简化IEnumerator的实现,引入了yield语句。

  • 简单来说,yield return用于在每次迭代时返回一个元素,这个元素可以通过IEnumerator.Current属性访问;
  • 同时,yield语句会保留迭代器的当前状态,当下次迭代时,保证状态连续性。

参考:UntiyC#编程:Coroutine协同程序

理解一:C#中的迭代器:C#--IEnumerable 与 IEnumerator 的区别

  1. static void Main(string[] args)
  2. {
    // 1. define array
  3.   ArrayList arrStus = new ArrayList
  4.   {
  5.     new Student("", "liuliu"," little rabbit"),
  6.     new Student("", "zhangsan", "little tortoise")
  7.   };
  8.   // 2. List<T> 继承了IEnumerable<T>, IEnumerble<T>继承了IEnumerable.
  9.   List<Student> stuL = ArrListToArr<Student>(arrStus);
  10.   foreach(Student stu in stuL)
  11.   {
  12.     Console.WriteLine($"{ stu.Name + " " + stu.Id + " " + stu.Remarks }");
  13.   };
  14. }
  15.  
  16. static List<T> ArrListToArr<T>(ArrayList arrL)
  17. {
  18.   List<T> list = new List<T>();
  19.   // 3. ArrList 定义时已继承了IEnumerable
  20.   IEnumerator enumerator = arrL.GetEnumerator();
  21.  
  22.   while (enumerator.MoveNext())
  23.   {
  24.     T item = (T)(enumerator.Current);
  25.     list.Add(item);
  26.   }
  27.  
  28.   return list;
  29. }

理解二:进一步的,unity中协程的使用。

执行顺序是12435

  • 执行到4协程注册了事件,控制权交给外部线程;
  • 外部线程执行到3;
  • 事件发生,程序分段执行机制goto到协程处记录了堆栈信息执行5语句。
  1. voidStart()
  2. {
  3.   print("Starting " +Time.time);---------------------------------------
  4.   StartCoroutine(WaitAndPrint());-------------------------------------
  5.   print("Done " +Time.time);-------------------------------------------
  6. }
  7.  
  8. IEnumerator WaitAndPrint(float waitTime)
  9. {
  10.   yield return new WaitForSeconds(waitTime);---------------------------
  11.   print("WaitAndPrint " + Time.time);----------------------------------
  12. }

执行顺序是12453

  • 程序执行到4,执行yield return表达式注册事件交出控制权给外部,
  • 因为外部还要交出控制权也需要执行yield return后面的表达式语句因此会重入WaitAndPrint函数接着协程当前状态下一步执行所以执行到5,
  • yield return 后面表达式语句执行完毕控制权完全交出,之后才执行3,
  • 根本原因是yield return 不能直接嵌套,后面需要跟一个表达式(事件)。
  1. IEnumerator Start()
  2. {
  3.   print("Starting " +Time.time);----------------------------------------
  4.   yield return StartCoroutine(WaitAndPrint(2.0F));----------------------
  5.   print("Done " +Time.time);--------------------------------------------
  6. }
  7.  
  8. IEnumerator WaitAndPrint(float waitTime)
  9. {
  10.   yield return new WaitForSeconds(waitTime);----------------------------
  11.   print("WaitAndPrint " + Time.time);-----------------------------------
  12. }

15.

参考:【Unity技巧】四元数(Quaternion)和旋转

16.

理解为:函数指针,可以把方法当作参数传递。

与C或C++中的函数指针不同,委托是面向对象、类型安全的,并且是安全的。

  1. public class DelegateScript : MonoBehaviour
  2. {
  3. delegate void MyDelegate(int num);
  4. MyDelegate myDelegate;
  5.  
  6. void Start ()
  7. {
  8. myDelegate = PrintNum;
  9. myDelegate();
  10.  
  11. myDelegate = DoubleNum;
  12. myDelegate();
  13. }
  14.  
  15. void PrintNum(int num)
  16. {
  17. print ("Print Num: " + num);
  18. }
  19.  
  20. void DoubleNum(int num)
  21. {
  22. print ("Double Num: " + num * );
  23. }
  24. }

如果是函数指针数组的角色:

  1. public class MulticastScript : MonoBehaviour
  2. {
  3. delegate void MultiDelegate();
  4. MultiDelegate myMultiDelegate;
  5.  
  6. void Start ()
  7. {
  8. myMultiDelegate += PowerUp;
  9. myMultiDelegate += TurnRed;
  10.  
  11. if(myMultiDelegate != null)
  12. {
  13. myMultiDelegate();
  14. }
  15. }
  16.  
  17. void PowerUp()
  18. {
  19. print ("Orb is powering up!");
  20. }
  21.  
  22. void TurnRed()
  23. {
  24. renderer.material.color = Color.red;
  25. }
  26. }

17.

  1. public class SpinScript : MonoBehaviour
  2. {
  3. [Range(-100, 100)]
    public int speed = ;
  4.  
  5. void Update ()
  6. {
  7. transform.Rotate(new Vector3(, speed * Time.deltaTime, ));
  8. }
  9. }

  1. [ExecuteInEditMode]
  2. public class ColorScript : MonoBehaviour
  3. {
  4. void Start()
  5. {
  6. renderer.sharedMaterial.color = Color.red;
  7. }
  8. }

Unity中默认情况下,脚本只有在运行的时候才被执行,加上此属性后,不运行程序,也能执行脚本。

18.

参考:C# Event/UnityEvent辨析

Event为喜爱他的观众(具有相同函数类型的函数)提供了订阅他的途径(即把自身加入到event的函数容器中),

这样无论他有什么动向,都可以直接通知所有他知道的粉丝(调用event会立即引用所有函数容器中的函数)。

event只负责告诉每个函数什么时候被调用,这些函数到底干了什么,event并不关心。

  1. public class Idol : MonoBehaviour {
  2. public delegate void IdolBehaviour(string behaviour);
  3. public static event IdolBehaviour IdolDoSomethingHandler;
  4.  
  5. private void Start()
  6. {
  7. //Idol 决定搞事了, 如果他还有粉丝的话, 就必须全部都通知到
  8. if (IdolDoSomethingHandler != null)
  9. {
  10. IdolDoSomethingHandler("Idol give up writing.");
  11. }
  12. }
  13. }

提前订阅了Idol:

  1. public class SubscriberA : MonoBehaviour {
  2. /// OnEnable在该脚本被启用时调用,你可以把它看做路转粉的开端
  3. private void OnEnable()
  4. {
  5. Idol.IdolDoSomethingHandler += LikeIdol;
  6. }
  7.  
  8. /// OnEnable在该脚本被禁用时调用,你可以把它看做粉转路的开端
  9. private void OnDisable()
  10. {
  11. Idol.IdolDoSomethingHandler -= LikeIdol;
  12. }

  13. /// 粉丝A是一个脑残粉
  14. public void LikeIdol(string idolAction)
  15. {
  16. print(idolAction + " I will support you forever!");
  17. }
  18. }

提前订阅了Idol:

  1. public class SubscriberB : MonoBehaviour {
  2.  
  3. /// OnEnable在该脚本被启用时调用,你可以把它看做路转粉的开端
  4. private void OnEnable()
  5. {
  6. Idol.IdolDoSomethingHandler += HateIdol;
  7. }
  8.  
  9. /// OnEnable在该脚本被禁用时调用,你可以把它看做粉转路的开端
  10. private void OnDisable()
  11. {
  12. Idol.IdolDoSomethingHandler -= HateIdol;
  13. }
  14.  
  15. /// 粉丝B是一个无脑黑
  16. public void HateIdol(string idolAction)
  17. {
  18. print(idolAction + " I will hate you forever!");
  19. }
  20. }

Unity Editor 和 Unity Engine的区别

前者是针对编辑器本身提供一些便于开发者编辑Unity物体的编程;后者则是具体到Unity要实现什么软件内容的编程。

  • UnityEditor是Unity编辑器编程,比如你要开发一个工具,一键给场景中所有的物体添加碰撞体,那么就可以用UnityEditor来开发这个工具,这个工具最简单的一个实现形态就是从Unity编辑器上方菜单栏拓展一个按钮,当按下这个按钮的时候,场景中所有的物体都会自动添加上碰撞体,相当于你手动给所有的物体添加碰撞体,注意这个时候场景是非运行状态(即编辑状态)。
  • UnityEngine是Unity编程,就是指你的具体软件内容开发了,比如你写了一个脚本功能是向前移动,那么你把这个脚本挂在一个Cube上面,当你的场景运行时Cube就会先前移动,注意这个时候场景是运行状态而非编辑状态了。

[Unity3D] C# Basic : Gameplay Scripting的更多相关文章

  1. Unity3d动画脚本 Animation Scripting(深入了解游戏引擎中的动画处理原理)

    也许这一篇文章的内容有点枯燥,但我要说的是如果你想深入的了解游戏引擎是如何处理动画片断或者素材并 让玩家操控的角色动起来栩栩如生,那么这真是一篇好文章(当然我仅仅是翻译了一下)   动画脚本 Anim ...

  2. FreeCodeCamp 的 Basic Algorithm Scripting 题解(1)

    这是本人的原创文章,转载请注明原文链接http://www.cnblogs.com/wusuowiaaa1blog/p/5932121.html. 1.Reverse a String 翻转字符串 先 ...

  3. [JS] JS Basic : compare with c#

    Ref: React从入门到精通视频教程 Ref: C# 教程 Ref: [Unity3D] C# Basic : Gameplay Scripting /* 之前的js总结有点low, 这次通过对比 ...

  4. [Code::Blocks] Install wxWidgets & openCV

    The open source, cross platform, free C++ IDE. Code::Blocks is a free C++ IDE built to meet the most ...

  5. 本人SW知识体系导航 - Programming menu

    将感悟心得记于此,重启程序员模式. js, py, c++, java, php 融汇之全栈系列 [Full-stack] 快速上手开发 - React [Full-stack] 状态管理技巧 - R ...

  6. Unity 官方教程 学习

    Interface & Essentials Using the Unity Interface 1.Interface Overview https://unity3d.com/cn/lea ...

  7. Awesome C/C++

    Awesome C/C++ A curated list of awesome C/C++ frameworks, libraries, resources, and shiny things. In ...

  8. awesome cpp

    https://github.com/fffaraz/awesome-cpp Awesome C/C++ A curated list of awesome C/C++ frameworks, lib ...

  9. [转]awsome c++

    原文链接 Awesome C++ A curated list of awesome C++ (or C) frameworks, libraries, resources, and shiny th ...

随机推荐

  1. Qt 4.6.2静态编译后,创建工程出现中文乱码的解决办法

    一.如果静态编译是用mingw编译的 1)在pro文件里增加QTPLUGIN += qcncodecs 2)在main函数所在的文件里面增加#include <QtPlugin>和Q_IM ...

  2. 读写锁--DEMO

    package com.demo.read.write.lock; import java.util.HashMap; import java.util.Map; import java.util.c ...

  3. spring-data-jpa中findOne与getOne的区别 getOne没数据 findOne有数据

    项目中用到了spring-data-jpa,今天在写一个update方法的时候报了个空指针,看了看是因为一个对象中的关联没有取出来,我用的是getOne取得这个对象,加断点看以一下这个对象是个hibe ...

  4. chapter15中使用generator来实现异步化操作的同步化表达的例子

    在p203中作者给了一个例子,我感觉这个例子写的不好,一开始我没有看懂,因为中间有很多细节没有交代,直到看了第二个用generator来实现ajax的例子之后才有所领悟.   所以我把作者给的这个用g ...

  5. 如何用 async 控制流程

    来自: http://larry850806.github.io/2016/05/31/async/ [Javascript] 如何用 async 控制流程 (一) 31 May 2016 async ...

  6. springcloud学习笔记(五)Spring Cloud Actuator

    <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring- ...

  7. JPA中自动使用@Table(name = "userTab")后自动将表名、列名添加了下划线的问题

    一.问题 JPA中自动使用@Table(name = "userTab")后自动将表名.列名添加了下划线的问题,如下图: 二.解决 在application.properties文 ...

  8. V-rep学习笔记:转动关节2

    Torque or force mode: in this mode, the joint is simulated by the dynamics module, if and only if it ...

  9. spring-mybatis代码生成插件,与实例展示

    前段时间看了张开涛写的代码生成插件,感觉思路很好,通过连接库然后获取数据库表信息,然后用户在界面中勾选要映射的策略,映射的字段,然后可以自动生成业务代码. 基于开涛的思路,自己写了一个简易插件,去掉了 ...

  10. 基于spring-mybatis-data-common基架快速搭建web应用

    spring-mybatis-data-common做了哪些操作 1.日志依据层级归类输出,支持扩展 2.spring-mybatis持久层基础接口集成,支持扩展 3.常用业务接口定义,支持扩展. 只 ...