HotReloadBasicDemo.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. #if ENABLE_MONO && (DEVELOPMENT_BUILD || UNITY_EDITOR)
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using UnityEngine;
  7. using UnityEngine.UI;
  8. namespace SingularityGroup.HotReload.Demo {
  9. class HotReloadBasicDemo : MonoBehaviour {
  10. public GameObject cube;
  11. public Text informationText;
  12. public Button openWindowButton;
  13. public Button openScriptButton;
  14. public TextAsset thisScript;
  15. // // 1. Adding fields (Added fields can show in the inspector)
  16. // public int myNewField = 1;
  17. void Start() {
  18. if (Application.isEditor) {
  19. openWindowButton.onClick.AddListener(Demo.I.OpenHotReloadWindow);
  20. openScriptButton.onClick.AddListener(() => Demo.I.OpenScriptFile(thisScript, myStaticField, 13));
  21. } else {
  22. openWindowButton.gameObject.SetActive(false);
  23. openScriptButton.gameObject.SetActive(false);
  24. informationText.gameObject.SetActive(false);
  25. }
  26. }
  27. // Update is called once per frame
  28. void Update() {
  29. if (Demo.I.IsServerRunning()) {
  30. informationText.text = "Hot Reload is running";
  31. } else {
  32. informationText.text = "Hot Reload is not running";
  33. }
  34. // // 2. Editing functions in monobehaviours, normal classes or static classes
  35. // // Edit the vector to rotate the cube in the scene differently or change the speed
  36. // var speed = 100;
  37. // cube.transform.Rotate(new Vector3(0, 1, 0) * Time.deltaTime * speed);
  38. // // 2. Editing functions in monobehaviours, normal classes or static classes
  39. // // Uncomment this code to scale the cube
  40. // cube.transform.localScale = Mathf.Sin(Time.time) * Vector3.one;
  41. // // 2. Editing functions in monobehaviours, normal classes or static classes
  42. // // Uncomment this code to make the cube move from left to right and back
  43. // var newPos = cube.transform.position += (cube.transform.localScale.x < 0.5 ? Vector3.left : Vector3.right) * Time.deltaTime;
  44. // if(Mathf.Abs(newPos.x) > 10) {
  45. // cube.transform.position = Vector3.zero;
  46. // }
  47. }
  48. // 3. Editing lambda methods
  49. static Func<int, int> addFunction = x => {
  50. var result = x + 10;
  51. Debug.Log("Add: " + result);
  52. // // uncomment to change the operator to multiply and log the result
  53. // result = x * 10;
  54. // Debug.Log("Multiply: " + result);
  55. return result;
  56. };
  57. // 4. Editing async/await methods
  58. async Task AsyncMethod() {
  59. // await Task.Delay(500);
  60. // Debug.Log("AsyncMethod");
  61. // // silicense warning
  62. await Task.CompletedTask;
  63. }
  64. // 5. Editing properties (get/set)
  65. public static string SomeString {
  66. // edit the get method
  67. get {
  68. var someStringHere = "This is some string";
  69. return someStringHere;
  70. }
  71. }
  72. // 6. Editing indexers (square bracket access such as dictionaries)
  73. class CustomDictionary : Dictionary<string, int> {
  74. public new int this[string key] {
  75. get {
  76. // // uncomment to change the indexer and log a different entry based on case
  77. // return base[key.ToLower()];
  78. return base[key.ToUpper()];
  79. }
  80. set {
  81. base[key.ToUpper()] = value;
  82. }
  83. }
  84. }
  85. CustomDictionary randomDict = new CustomDictionary {
  86. { "a", 4 },
  87. { "A", 5 },
  88. { "b", 9 },
  89. { "B", 10 },
  90. { "c", 14 },
  91. { "C", 15 },
  92. { "d", 19 },
  93. { "D", 20 }
  94. };
  95. // 7. Editing operators methods (explicit and implicit operators)
  96. public class Email {
  97. public string Value { get; }
  98. public Email(string value) {
  99. Value = value;
  100. }
  101. // Define implicit operator
  102. public static implicit operator string(Email value)
  103. // Uncomment to change the implicit operator
  104. // => value.Value + " FOO";
  105. => value.Value;
  106. // // Uncomment to change add an implicit operator
  107. // public static implicit operator byte[](Email value)
  108. // => Encoding.UTF8.GetBytes(value.Value);
  109. // Define explicit operator
  110. public static explicit operator Email(string value)
  111. => new Email(value);
  112. }
  113. // 8. Editing fields: modifiers/type/name/initializer
  114. public int myEditedField = 4;
  115. // 9. Editing static field initializers (variable value is updated)
  116. static readonly int myStaticField = 31;
  117. // // 10. Adding auto properties/events
  118. // int MyProperty { get; set; } = 6;
  119. // event Action MyEvent = () => Debug.Log("MyEvent");
  120. class GenericClass<T> {
  121. // // 11. Adding methods in generic classes
  122. // public void GenericMethod() {
  123. // Debug.Log("GenericMethod");
  124. // }
  125. // // 12. Adding fields (any type) in generic classes
  126. // public T myGenericField;
  127. }
  128. void LateUpdate() {
  129. // // 3. Editing lambda methods
  130. // addFunction(10);
  131. // // 4. Editing async/await methods
  132. // AsyncMethod().Forget();
  133. // // 5. Editing properties (get/set)
  134. // Debug.Log(SomeString);
  135. // // 6. Editing indexers (square bracket access such as dictionaries)
  136. // Debug.Log(randomDict["A"]);
  137. // // 7. Editing operators methods (explicit and implicit operators)
  138. Email email = new Email("example@example.com");
  139. // string stringEmail = email;
  140. // Debug.Log(stringEmail);
  141. // // Uncomment new operator in Email class + Uncomment this to add byte implicit operator
  142. // byte[] byteEmail = email;
  143. // var hexRepresentation = BitConverter.ToString(byteEmail);
  144. // Debug.Log(hexRepresentation);
  145. // Debug.Log(Encoding.UTF8.GetString(byteEmail));
  146. // // 8. Editing fields: modifiers/type/name/initializer
  147. // Debug.Log("myEditedField: " + myEditedField);
  148. // // 9. Editing static field initializers (variable value is updated)
  149. // Debug.Log("myStaticField: " + myStaticField);
  150. // // 10. Adding auto properties/events
  151. // Debug.Log("MyProperty: " + MyProperty);
  152. // MyEvent.Invoke();
  153. // var newClass = new GenericClass<int>();
  154. // // 11. Adding methods in generic classes
  155. // newClass.GenericMethod();
  156. // // 12. Adding fields in generic classes
  157. // newClass.myGenericField = 3;
  158. // Debug.Log("myGenericField: " + newClass.myGenericField);
  159. // // 13. Editing lambda methods with closures
  160. // // Uncomment to log sorted array
  161. // // Switch a and b to reverse the sorting
  162. // int[] numbers = { 5, 3, 8, 1, 9 };
  163. // Array.Sort(numbers, (b, a) => a.CompareTo(b));
  164. // Debug.Log(string.Join(", ", numbers));
  165. }
  166. // This function gets invoked every time it's patched
  167. [InvokeOnHotReloadLocal]
  168. static void OnHotReloadMe() {
  169. // // change the string to see the method getting invoked
  170. // Debug.Log("Hello there");
  171. }
  172. // // 14. Adding event functions
  173. // void OnDisable() {
  174. // Debug.Log("OnDisable");
  175. // }
  176. }
  177. }
  178. #endif