PageRenderTime 119ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/Assets/JMO Assets/Toony Colors Pro/Demo TCP2/Common Demo Assets/Scripts/TCP2_Demo_Interactive.cs

https://gitlab.com/nicolas.diazb97/chrome-score
C# | 357 lines | 272 code | 69 blank | 16 comment | 26 complexity | da3ec30e43486290ea626730a0758ed4 MD5 | raw file
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Rendering;
  5. using UnityEngine.UI;
  6. namespace ToonyColorsPro
  7. {
  8. namespace Demo
  9. {
  10. public class TCP2_Demo_Interactive : MonoBehaviour
  11. {
  12. public TCP2_Demo_Camera demoCamera;
  13. new public Camera camera;
  14. public Canvas canvas;
  15. public CanvasScaler canvasScaler;
  16. [Space]
  17. public HorizontalLayoutGroup layoutGroup;
  18. public ContentSizeFitter sizeFitter;
  19. [Space]
  20. public RectTransform textBox;
  21. public Text text;
  22. public Image line;
  23. [Space]
  24. public float camAnimDuration = 0.5f;
  25. public float maxCamAnimDuration = 1.0f;
  26. public bool camAnimBasedOnDistance = false;
  27. public float uiAnimDuration = 0.5f;
  28. [Space]
  29. public Button envButtonTemplate;
  30. Button[] envButtons;
  31. public Text highlightLabel;
  32. TCP2_Demo_Interactive_Content[] contents;
  33. TCP2_Demo_Interactive_Content currentContent;
  34. int index = -1;
  35. TCP2_Demo_Interactive_Environment[] lightings;
  36. int lightingIndex = 0;
  37. Color envButtonColor;
  38. Vector3 cameraResetPos;
  39. Quaternion cameraResetQuat;
  40. Transform resetPivot;
  41. void Awake()
  42. {
  43. contents = this.GetComponentsInChildren<TCP2_Demo_Interactive_Content>();
  44. lightings = this.GetComponentsInChildren<TCP2_Demo_Interactive_Environment>(true);
  45. if (QualitySettings.activeColorSpace == ColorSpace.Gamma)
  46. {
  47. foreach (var lighting in lightings)
  48. {
  49. var lights = lighting.GetComponentsInChildren<Light>();
  50. foreach (var light in lights)
  51. {
  52. light.intensity = light.intensity * 0.6f;
  53. }
  54. }
  55. RenderSettings.ambientIntensity = 0.6f;
  56. RenderSettings.reflectionIntensity = 0.6f;
  57. }
  58. envButtonColor = envButtonTemplate.GetComponent<Image>().color;
  59. envButtons = new Button[lightings.Length];
  60. for (int i = 0; i < lightings.Length; i++)
  61. {
  62. var btnGo = GameObject.Instantiate(envButtonTemplate.gameObject);
  63. btnGo.name = envButtonTemplate.name + "_" + i;
  64. btnGo.transform.SetParent(envButtonTemplate.transform.parent);
  65. btnGo.transform.SetSiblingIndex(envButtonTemplate.transform.GetSiblingIndex());
  66. var text = btnGo.GetComponentInChildren<Text>();
  67. text.text = lightings[i].name;
  68. var btn = btnGo.GetComponent<Button>();
  69. int ci = i;
  70. btn.onClick.AddListener(new UnityEngine.Events.UnityAction(() => { this.OnSelectLightingSettings(ci); }));
  71. envButtons[i] = btn;
  72. }
  73. envButtonTemplate.gameObject.SetActive(false);
  74. OnSelectLightingSettings(0);
  75. cameraResetPos = camera.transform.position;
  76. cameraResetQuat = camera.transform.rotation;
  77. resetPivot = demoCamera.Pivot;
  78. }
  79. void LateUpdate()
  80. {
  81. HandleKeyboard();
  82. if (index >= 0 && !coroutineActive)
  83. {
  84. UpdateViewToCurrentContent();
  85. }
  86. }
  87. void HandleKeyboard()
  88. {
  89. if (Input.GetKeyDown(KeyCode.Delete) || Input.GetKeyDown(KeyCode.H))
  90. {
  91. canvas.enabled = !canvas.enabled;
  92. }
  93. if (Input.GetKeyDown(KeyCode.Escape))
  94. {
  95. ResetView();
  96. }
  97. if (Input.GetKeyDown(KeyCode.RightArrow))
  98. {
  99. NextHighlight();
  100. }
  101. if (Input.GetKeyDown(KeyCode.LeftArrow))
  102. {
  103. PrevHighlight();
  104. }
  105. if(Input.GetKeyDown(KeyCode.Tab))
  106. {
  107. if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift))
  108. {
  109. lightingIndex--;
  110. if (lightingIndex < 0) lightingIndex = envButtons.Length - 1;
  111. }
  112. else
  113. {
  114. lightingIndex++;
  115. if (lightingIndex >= envButtons.Length) lightingIndex = 0;
  116. }
  117. OnSelectLightingSettings(lightingIndex);
  118. }
  119. }
  120. public void PrevHighlight()
  121. {
  122. index--;
  123. if (index < 0) index = contents.Length - 1;
  124. StopAllCoroutines();
  125. StartCoroutine(CR_MoveToContent(contents[index]));
  126. highlightLabel.text = contents[index].name;
  127. }
  128. public void NextHighlight()
  129. {
  130. index++;
  131. if (index >= contents.Length) index = 0;
  132. StopAllCoroutines();
  133. StartCoroutine(CR_MoveToContent(contents[index]));
  134. highlightLabel.text = contents[index].name;
  135. }
  136. void UpdateViewToCurrentContent(float lengthPercent = 1.0f)
  137. {
  138. var screenPos = camera.WorldToScreenPoint(currentContent.pivot.position);
  139. var endPos = camera.WorldToScreenPoint(currentContent.textBox.position);
  140. textBox.position = endPos;
  141. var w2 = textBox.rect.width / 2.0f;
  142. var h2 = textBox.rect.height / 2.0f;
  143. // check if text box exceeds screen bounds
  144. if (endPos.x - w2 < 0)
  145. {
  146. endPos.x = w2;
  147. }
  148. if (endPos.x + w2 > Screen.width)
  149. {
  150. endPos.x = Screen.width - w2;
  151. }
  152. if (endPos.y - h2 < 0)
  153. {
  154. endPos.y = h2;
  155. }
  156. if (endPos.y + h2 > Screen.height)
  157. {
  158. endPos.y = Screen.height - h2;
  159. }
  160. textBox.position = endPos;
  161. PlaceLine(endPos, screenPos, lengthPercent);
  162. }
  163. void PlaceLine(Vector2 start, Vector2 end, float lengthPercentage)
  164. {
  165. line.rectTransform.position = start;
  166. start.y = -start.y;
  167. end.y = -end.y;
  168. float angle = Vector2.SignedAngle((start - end).normalized, Vector2.up);
  169. var r = line.rectTransform.localEulerAngles;
  170. r.z = angle;
  171. line.rectTransform.localEulerAngles = r;
  172. // Need to find canvas ratio according to resolution for proper distance calculation
  173. float ratio = canvasScaler.referenceResolution.x / Screen.width;
  174. float dist = Vector2.Distance(start, end) * lengthPercentage;
  175. dist *= ratio;
  176. var sd = line.rectTransform.sizeDelta;
  177. sd.y = dist;
  178. line.rectTransform.sizeDelta = sd;
  179. }
  180. void ResetView()
  181. {
  182. canvas.enabled = false;
  183. StopAllCoroutines();
  184. StartCoroutine(CR_ResetCamPos());
  185. highlightLabel.text = "...";
  186. }
  187. IEnumerator CR_ResetCamPos()
  188. {
  189. // --------------------------------
  190. // Animate Camera
  191. demoCamera.Pivot = resetPivot;
  192. demoCamera.pivotOffset = Vector3.zero;
  193. Vector3 startPos = camera.transform.position;
  194. Vector3 endPos = cameraResetPos;
  195. Quaternion startQuat = camera.transform.rotation;
  196. Quaternion endQuat = cameraResetQuat;
  197. float duration = camAnimBasedOnDistance ? Vector3.Distance(startPos, endPos) * camAnimDuration : camAnimDuration;
  198. duration = Mathf.Min(duration, maxCamAnimDuration);
  199. float time = duration;
  200. while (time > 0)
  201. {
  202. time -= Time.deltaTime;
  203. yield return null;
  204. float delta = Mathf.SmoothStep(0, 1, 1 - Mathf.Clamp01(time/duration));
  205. camera.transform.position = Vector3.Lerp(startPos, endPos, delta);
  206. camera.transform.rotation = Quaternion.Slerp(startQuat, endQuat, delta);
  207. }
  208. }
  209. bool coroutineActive;
  210. IEnumerator CR_MoveToContent(TCP2_Demo_Interactive_Content content)
  211. {
  212. coroutineActive = true;
  213. // Hide UI
  214. canvas.enabled = false;
  215. // --------------------------------
  216. // Animate Camera
  217. Vector3 startPos = camera.transform.position;
  218. Vector3 endPos = content.transform.position;
  219. Quaternion startQuat = camera.transform.rotation;
  220. Quaternion endQuat = content.transform.rotation;
  221. float duration = camAnimBasedOnDistance ? Vector3.Distance(startPos, endPos) * camAnimDuration : camAnimDuration;
  222. duration = Mathf.Min(duration, maxCamAnimDuration);
  223. float time = duration;
  224. while (time > 0)
  225. {
  226. time -= Time.deltaTime;
  227. yield return null;
  228. float delta = Mathf.SmoothStep(0, 1, 1 - Mathf.Clamp01(time/duration));
  229. camera.transform.position = Vector3.Lerp(startPos, endPos, delta);
  230. camera.transform.rotation = Quaternion.Slerp(startQuat, endQuat, delta);
  231. }
  232. currentContent = contents[index];
  233. camera.transform.position = currentContent.transform.position;
  234. camera.transform.rotation = currentContent.transform.rotation;
  235. demoCamera.Pivot = currentContent.pivot;
  236. demoCamera.pivotOffset = Vector3.zero;
  237. text.text = currentContent.Text;
  238. UpdateViewToCurrentContent(0f);
  239. yield return null;
  240. UpdateViewToCurrentContent(0f);
  241. yield return null;
  242. UpdateViewToCurrentContent(0f);
  243. // --------------------------------
  244. // Resize the text box according to its content
  245. layoutGroup.enabled = true;
  246. sizeFitter.enabled = true;
  247. yield return null;
  248. // Disable text box resizing as content won't change
  249. layoutGroup.enabled = false;
  250. sizeFitter.enabled = false;
  251. // Show UI
  252. canvas.enabled = true;
  253. // --------------------------------
  254. // Animate UI
  255. textBox.localScale = Vector3.zero;
  256. duration = uiAnimDuration;
  257. time = duration;
  258. while (time > 0)
  259. {
  260. time -= Time.deltaTime;
  261. yield return null;
  262. float delta = Mathf.SmoothStep(0, 1, 1 - Mathf.Clamp01(time/duration));
  263. // Text box
  264. textBox.localScale = Vector3.Lerp(Vector3.zero, Vector3.one, delta);
  265. line.rectTransform.localScale = new Vector3(1 / textBox.localScale.x, 1 / textBox.localScale.y, 1);
  266. // Line length + repositioning
  267. UpdateViewToCurrentContent(delta);
  268. }
  269. // --------------------------------
  270. coroutineActive = false;
  271. }
  272. void OnSelectLightingSettings(int index)
  273. {
  274. lightingIndex = index;
  275. lightings[index].ApplyEnvironment();
  276. for (int i = 0; i < envButtons.Length; i++)
  277. {
  278. envButtons[i].GetComponent<Image>().color = (i == index) ? new Color(0.6f, 0.2f, 0.0f) : envButtonColor;
  279. }
  280. }
  281. public GameObject infoBox;
  282. public void HideInfoBox()
  283. {
  284. infoBox.SetActive(false);
  285. }
  286. }
  287. }
  288. }