Tanoda
LeapRenderingMethod.cs
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (C) Ultraleap, Inc. 2011-2020. *
3 * *
4 * Use subject to the terms of the Apache License 2.0 available at *
5 * http://www.apache.org/licenses/LICENSE-2.0, or another agreement *
6 * between Ultraleap and you, your company or other organization. *
7 ******************************************************************************/
8
9using System;
10using System.Collections.Generic;
11using UnityEngine;
12using UnityEngine.Assertions;
13using UnityObject = UnityEngine.Object;
14#if UNITY_EDITOR
15using UnityEditor;
16#endif
17using Leap.Unity.Space;
18
20
24 }
25
26 [Serializable]
27 public abstract class LeapRenderingMethod : ILeapInternalRenderingMethod {
28 public const string DATA_FOLDER_NAME = "_ElementData";
29
30 [NonSerialized]
31 private LeapGraphicRenderer _renderer;
32
33 [NonSerialized]
34 private LeapGraphicGroup _group;
35
40 get {
41 return _renderer;
42 }
43 }
44
49 get {
50 return _group;
51 }
52 }
53
58 set {
59 _renderer = value;
60 }
61 }
62
66 LeapGraphicGroup ILeapInternalRenderingMethod.group {
67 set {
68 _group = value;
69 }
70 }
71
73
77 public abstract void OnEnableRenderer();
78
82 public abstract void OnDisableRenderer();
83
88 public abstract void OnUpdateRenderer();
89
90#if UNITY_EDITOR
95 public virtual void OnEnableRendererEditor() {
96 }
97
102 public virtual void OnDisableRendererEditor() {
103 }
104
110 public virtual void OnUpdateRendererEditor() { }
111#endif
112
113 public abstract bool IsValidGraphic<T>();
114 public abstract bool IsValidGraphic(LeapGraphic graphic);
115
116 public abstract LeapGraphic GetValidGraphicOnObject(GameObject obj);
117
118 private static Dictionary<UnityObject, object> _assetToOwner = new Dictionary<UnityObject, object>();
119 public void PreventDuplication<T>(ref T t) where T : UnityObject {
120 Assert.IsNotNull(t);
121
122 object owner;
123 if (_assetToOwner.TryGetValue(t, out owner)) {
124 if (owner.Equals(this)) {
125 return;
126 }
127
128 if (t is Texture2D) {
129 Texture2D tex = t as Texture2D;
130
131 RenderTexture rt = new RenderTexture(tex.width, tex.height, 0, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Linear);
132 Graphics.Blit(tex, rt);
133
134 Texture2D newTex = new Texture2D(tex.width, tex.height, TextureFormat.ARGB32, tex.mipmapCount > 1, true);
135 RenderTexture.active = rt;
136 newTex.ReadPixels(new Rect(0, 0, tex.width, tex.height), 0, 0);
137 newTex.Apply();
138 RenderTexture.active = null;
139 rt.Release();
140 UnityObject.DestroyImmediate(rt);
141
142 t = newTex as T;
143 } else {
144 t = UnityObject.Instantiate(t);
145 }
146 }
147 _assetToOwner[t] = this;
148 }
149 }
150
151 public abstract class LeapRenderingMethod<GraphicType> : LeapRenderingMethod
152 where GraphicType : LeapGraphic {
153 public const string ASSET_PATH = "Assets/Generated/RendererData/";
154
155 public override bool IsValidGraphic<T>() {
156 Type t = typeof(T);
157 Type graphicType = typeof(GraphicType);
158
159 return t == graphicType || (t.IsSubclassOf(graphicType));
160 }
161
162 public override bool IsValidGraphic(LeapGraphic graphic) {
163 return graphic is GraphicType;
164 }
165
166 public override LeapGraphic GetValidGraphicOnObject(GameObject obj) {
167 return obj.GetComponent<GraphicType>();
168 }
169 }
170}
UnityEngine.Object UnityObject
abstract void OnUpdateRenderer()
Called from LateUpdate during runtime. Use this to update the renderer using any changes made to duri...
LeapGraphicGroup group
Gets the group this rendering method is attached to.
abstract void OnDisableRenderer()
Called when the renderer is disabled at runtime.
LeapGraphicRenderer renderer
Gets the renderer this rendering method is attached to.
abstract bool IsValidGraphic(LeapGraphic graphic)
override LeapGraphic GetValidGraphicOnObject(GameObject obj)
abstract LeapGraphic GetValidGraphicOnObject(GameObject obj)
override bool IsValidGraphic(LeapGraphic graphic)
abstract void OnEnableRenderer()
Called when the renderer is enabled at runtime.
abstract SupportInfo GetSpaceSupportInfo(LeapSpace space)
UnityEngine.Object UnityObject
Definition: EditorUtils.cs:19
The support info class provides a very basic way to notify that something is fully supported,...
Definition: SupportInfo.cs:21