Locale









How do I use Locale
Below are practical examples compiled from projects for learning and reference purposes

Featured Snippets


File name: AnimationBuilder.cs Copy
222         private void SetGameObjectForRef(GameObject root, Ref childRef, float time)
223         {
224             TimelineKey key = childRef.Referenced;
225             if (time < 0) time = key.Time;
226
227             TimelineKey lastKey;
228             lastKeyframeCache.TryGetValue(key.Timeline, out lastKey);
229
230             //Get the relative path based on the current hierarchy
231             var relativePath = childRef.RelativePath;
232
233             //If this is the root, skip it
234             if (string.IsNullOrEmpty(relativePath))
235             {
236                 Debug.Log("Skipping root node in SetGameObjectForRef (SHOULD NEVER HAPPEN)");
237                 return;
238             }
239
240
241             //Find the gameObject based on relative path
242             var transform = root.transform.Find(relativePath);
243             if (transform == null)
244             {
245                 Debug.LogError("ERROR: Unable to find GameObject at relative path " + relativePath);
246                 return;
247             }
248
249             var gameObject = transform.gameObject;
250             gameObject.SetActive(true);
251
252             //Get transform data from ref
253             Vector3 localPosition;
254             Vector3 localScale;
255             Vector3 localEulerAngles;
256
257             childRef.BakeTransforms(out localPosition, out localEulerAngles, out localScale);
258
259             //Set the current GameObject's transform data
260             transform.localPosition = localPosition;
261             transform.localScale = localScale;
262
263             //Spin the object in the correct direction
264             var oldEulerAngles = transform.localEulerAngles;
265
266             if (oldEulerAngles.z - localEulerAngles.z > 180) localEulerAngles.z += 360;
267             else if (localEulerAngles.z - oldEulerAngles.z > 180) localEulerAngles.z -= 360;
268             /*
269             switch(childRef.Unmapped.Spin)
270             {
271                 case SpinDirection.Clockwise:
272                     while (oldEulerAngles.z > localEulerAngles.z) localEulerAngles.z += 360;
273                     break;
274                 case SpinDirection.CounterClockwise:
275                     while (oldEulerAngles.z < localEulerAngles.z) localEulerAngles.z -= 360;
276                     break;
277             }*/
278             transform.localEulerAngles = localEulerAngles;
279
280             int zIndex = -1;
281             var spriteKey = key as SpriteTimelineKey;
282             if (spriteKey != null)
283             {
284                 zIndex = ((ObjectRef)childRef).ZIndex;
285                 //transform.GetComponent().sortingOrder = zIndex;
286             }
287
288             acb.SetCurve(root.transform, transform, time, lastKey, zIndex);
289
290
291             //Get last-used game object for this Timeline - needed to clean up reparenting
292             GameObject lastGameObject;
293             if (lastGameObjectCache.TryGetValue(key.Timeline, out lastGameObject) && gameObject != lastGameObject)
294             {
295                 //Let Unity handle the global->local position cruft for us
296                 lastGameObject.transform.position = transform.position;
297                 lastGameObject.transform.eulerAngles = transform.eulerAngles;
298
299                 //TODO: Also need to do something about scale - this is a little more tricky
300                 lastGameObject.transform.localScale = localScale;
301
302                 //Deactivate the old object
303                 lastGameObject.SetActive(false);
304
305                 acb.SetCurve(root.transform, lastGameObject.transform, time, lastKey);
306             }
307
308             //Set cached value for last keyframe
309             lastKeyframeCache[key.Timeline] = key;
310         }
File name: AnimationCurveBuilder.cs Copy
128         private void UpdateTransformCurve(ObjectCurves obj, Transform current, float time, TimelineKey lastTimelineKey, int zIndex = -1)
129         {
130             float val;
131             //IsActive curve
132             val = (current.gameObject.activeSelf) ? 1.0f : 0.0f;
133             obj.Curves[(int)AnimationCurveIndex.IsActive].AddKey(new Keyframe(time, val, float.PositiveInfinity, float.PositiveInfinity) { tangentMode = 0 });
134
135             //Position curves
136             obj.Curves[(int)AnimationCurveIndex.LocalPositionX].AddKey(new Keyframe(time, current.localPosition.x) { tangentMode = 0 }, lastTimelineKey);
137             obj.Curves[(int)AnimationCurveIndex.LocalPositionY].AddKey(new Keyframe(time, current.localPosition.y) { tangentMode = 0 }, lastTimelineKey);
138             obj.Curves[(int)AnimationCurveIndex.LocalPositionZ].AddKey(new Keyframe(time, current.localPosition.z, float.PositiveInfinity, float.PositiveInfinity)); //Z value always has instant transition
139
140             //Rotation curves
141             var quat = Quaternion.Euler(current.localEulerAngles);
142             obj.Curves[(int)AnimationCurveIndex.LocalRotationX].AddKey(new Keyframe(time, quat.x) { tangentMode = 0 }, lastTimelineKey);
143             obj.Curves[(int)AnimationCurveIndex.LocalRotationY].AddKey(new Keyframe(time, quat.y) { tangentMode = 0 }, lastTimelineKey);
144             obj.Curves[(int)AnimationCurveIndex.LocalRotationZ].AddKey(new Keyframe(time, quat.z) { tangentMode = 0 }, lastTimelineKey);
145             obj.Curves[(int)AnimationCurveIndex.LocalRotationW].AddKey(new Keyframe(time, quat.w) { tangentMode = 0 }, lastTimelineKey);
146
147             //Scale curves
148             obj.Curves[(int)AnimationCurveIndex.LocalScaleX].AddKey(new Keyframe(time, current.localScale.x) { tangentMode = 0 }, lastTimelineKey);
149             obj.Curves[(int)AnimationCurveIndex.LocalScaleY].AddKey(new Keyframe(time, current.localScale.y) { tangentMode = 0 }, lastTimelineKey);
150             obj.Curves[(int)AnimationCurveIndex.LocalScaleZ].AddKey(new Keyframe(time, current.localScale.z) { tangentMode = 0 }, lastTimelineKey);
151
152             //Sprite Curves
153             var spriteTimelineKey = lastTimelineKey as SpriteTimelineKey;
154             if (spriteTimelineKey != null)
155             {
156                 obj.IsSpriteKey = true;
157                 obj.Curves[(int)AnimationCurveIndex.ColorR].AddKey(new Keyframe(time, spriteTimelineKey.Tint.r) { tangentMode = 0 }, lastTimelineKey);
158                 obj.Curves[(int)AnimationCurveIndex.ColorG].AddKey(new Keyframe(time, spriteTimelineKey.Tint.g) { tangentMode = 0 }, lastTimelineKey);
159                 obj.Curves[(int)AnimationCurveIndex.ColorB].AddKey(new Keyframe(time, spriteTimelineKey.Tint.b) { tangentMode = 0 }, lastTimelineKey);
160                 obj.Curves[(int)AnimationCurveIndex.ColorA].AddKey(new Keyframe(time, spriteTimelineKey.Tint.a) { tangentMode = 0 }, lastTimelineKey);
161                 obj.Curves[(int)AnimationCurveIndex.ZIndex].AddKey(new Keyframe(time, zIndex, float.PositiveInfinity, float.PositiveInfinity));
162             }
163         }
File name: PrefabBuilder.cs Copy
132         private void SetTransform(Ref childRef, Transform transform)
133         {
134             Vector3 localPosition;
135             Vector3 localScale;
136    Vector3 localEulerAngles;
137
138             PrefabUtils.BakeTransforms(childRef, out localPosition, out localEulerAngles, out localScale);
139             transform.localPosition = localPosition;
140             transform.localScale = localScale;
141    transform.localEulerAngles = localEulerAngles;
142         }
File name: PrefabUtils.cs Copy
34         public static void BakeTransforms(this Ref childRef, out Vector3 localPosition, out Vector3 localEulerAngles, out Vector3 localScale)
35         {
36             TimelineKey key = childRef.Referenced;
37
38             localPosition = Vector3.zero;
39             localScale = Vector3.one;
40             localEulerAngles = Vector3.zero;
41
42             var unmapped = childRef.Unmapped;
43             var spatial = childRef.Referenced as SpatialTimelineKey;
44             if (spatial != null)
45             {
46                 localPosition = unmapped.Position;
47
48                 var spriteKey = key as SpriteTimelineKey;
49                 if (spriteKey != null)
50                 {
51                     var sinA = Mathf.Sin(unmapped.Angle);
52                     var cosA = Mathf.Cos(unmapped.Angle);
53
54                     var pvt = spriteKey.GetPivotOffetFromMiddle();
55
56                     pvt.x *= unmapped.Scale.x;
57                     pvt.y *= unmapped.Scale.y;
58
59                     var rotPX = pvt.x * cosA - pvt.y * sinA;
60                     var rotPY = pvt.x * sinA + pvt.y * cosA;
61
62                     localPosition.x += rotPX;
63                     localPosition.y += rotPY;
64
65                     localScale.x = unmapped.Scale.x;
66                     localScale.y = unmapped.Scale.y;
67                     localPosition.z = ((ObjectRef)childRef).ZIndex * -Z_SPACING;
68                 }
69
70                 localPosition *= PIXEL_SCALE;
71                 localEulerAngles = new Vector3(0, 0, unmapped.Angle_Deg);
72             }
73         }

Download file with original file name:Locale

Locale 117 lượt xem

Gõ tìm kiếm nhanh...