Optimization: caching unity components
new actions: ActionBezierRel.cs ActionBlink.cs ActionJumpBy.cs ActionJumpTo.cs
This commit is contained in:
@@ -6,6 +6,8 @@ public class Action
|
||||
{
|
||||
protected Actor target;
|
||||
public float duration = 0;
|
||||
protected Transform transform;
|
||||
protected Renderer renderer;
|
||||
|
||||
public Action()
|
||||
{
|
||||
@@ -36,6 +38,8 @@ public class Action
|
||||
{
|
||||
if (target == null)
|
||||
throw new Exception("Can start the action only after it's atached to the actor");
|
||||
transform = target.gameObject.transform;
|
||||
renderer = target.gameObject.renderer;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -18,6 +18,6 @@ class ActionHide : Action
|
||||
public override void start()
|
||||
{
|
||||
base.start();
|
||||
target.gameObject.renderer.enabled = false;
|
||||
renderer.enabled = false;
|
||||
}
|
||||
}
|
||||
@@ -20,6 +20,6 @@ class ActionSetPlace : Action
|
||||
public override void start()
|
||||
{
|
||||
base.start();
|
||||
target.gameObject.transform.position = place;
|
||||
transform.position = place;
|
||||
}
|
||||
}
|
||||
@@ -23,10 +23,10 @@ class ActionSetRotation : Action
|
||||
Vector3 path = new Vector3();
|
||||
for (int i = 0; i < 3; i++)
|
||||
{
|
||||
path[i] = value[i] - target.gameObject.transform.rotation.eulerAngles[i];
|
||||
path[i] = value[i] - transform.rotation.eulerAngles[i];
|
||||
}
|
||||
target.gameObject.transform.Rotate(Vector3.up, path.y);
|
||||
target.gameObject.transform.Rotate(Vector3.right, path.x);
|
||||
target.gameObject.transform.Rotate(Vector3.forward, path.z);
|
||||
transform.Rotate(Vector3.up, path.y);
|
||||
transform.Rotate(Vector3.right, path.x);
|
||||
transform.Rotate(Vector3.forward, path.z);
|
||||
}
|
||||
}
|
||||
@@ -16,6 +16,6 @@ class ActionSetTint : Action
|
||||
public override void start()
|
||||
{
|
||||
base.start();
|
||||
target.gameObject.renderer.material.color = new Color(color[0], color[1], color[2], color[3]);
|
||||
renderer.material.color = new Color(color[0], color[1], color[2], color[3]);
|
||||
}
|
||||
}
|
||||
@@ -18,6 +18,6 @@ class ActionShow : Action
|
||||
public override void start()
|
||||
{
|
||||
base.start();
|
||||
target.gameObject.renderer.enabled = true;
|
||||
renderer.enabled = true;
|
||||
}
|
||||
}
|
||||
@@ -13,6 +13,6 @@ class ActionToggleVisibility : Action
|
||||
public override void start()
|
||||
{
|
||||
base.start();
|
||||
target.gameObject.renderer.enabled = !target.gameObject.renderer.enabled;
|
||||
renderer.enabled = !renderer.enabled;
|
||||
}
|
||||
}
|
||||
@@ -4,10 +4,10 @@ using UnityEngine;
|
||||
|
||||
class ActionBezierAbs : ActionInterval
|
||||
{
|
||||
public Vector3 startPoint;
|
||||
public Vector3 endPoint;
|
||||
public Vector3 startControlPoint;
|
||||
public Vector3 endControlPoint;
|
||||
protected Vector3 startPoint;
|
||||
protected Vector3 endPoint;
|
||||
protected Vector3 startControlPoint;
|
||||
protected Vector3 endControlPoint;
|
||||
|
||||
public ActionBezierAbs(Vector3 tgtStart, Vector3 tgtSCP, Vector3 tgtECP, Vector3 tgtEnd, float tgtDuration)
|
||||
: base(tgtDuration)
|
||||
@@ -28,11 +28,6 @@ class ActionBezierAbs : ActionInterval
|
||||
return new ActionBezierAbs(endPoint, endControlPoint, startControlPoint, startPoint, duration);
|
||||
}
|
||||
|
||||
public override void start()
|
||||
{
|
||||
base.start();
|
||||
}
|
||||
|
||||
public override void stepInterval(float dt)
|
||||
{
|
||||
float t = timer / duration;
|
||||
@@ -40,6 +35,6 @@ class ActionBezierAbs : ActionInterval
|
||||
+ 3 * (startControlPoint - endControlPoint) + endPoint) * t
|
||||
+ (3 * (startPoint + endControlPoint) - 6 * startControlPoint)) * t +
|
||||
3 * (startControlPoint - startPoint)) * t + startPoint;
|
||||
target.gameObject.transform.position = newPosition;
|
||||
transform.position = newPosition;
|
||||
}
|
||||
}
|
||||
51
src/ActionsInterval/ActionBezierRel.cs
Normal file
51
src/ActionsInterval/ActionBezierRel.cs
Normal file
@@ -0,0 +1,51 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
class ActionBezierRel : ActionInterval
|
||||
{
|
||||
protected Vector3 ep;
|
||||
protected Vector3 cp1;
|
||||
protected Vector3 cp2;
|
||||
protected Vector3 startPoint;
|
||||
protected Vector3 endPoint;
|
||||
protected Vector3 startControlPoint;
|
||||
protected Vector3 endControlPoint;
|
||||
|
||||
public ActionBezierRel(Vector3 tgtSCP, Vector3 tgtECP, Vector3 tgtEnd, float tgtDuration)
|
||||
: base(tgtDuration)
|
||||
{
|
||||
ep = tgtEnd;
|
||||
cp1 = tgtSCP;
|
||||
cp2 = tgtECP;
|
||||
}
|
||||
|
||||
public override Action clone()
|
||||
{
|
||||
return new ActionBezierRel(startControlPoint, endControlPoint, endPoint, duration);
|
||||
}
|
||||
|
||||
public override Action reverse()
|
||||
{
|
||||
return new ActionBezierRel(-startControlPoint, -endControlPoint, -endPoint, duration);
|
||||
}
|
||||
|
||||
public override void start()
|
||||
{
|
||||
base.start();
|
||||
startPoint = target.transform.position;
|
||||
endPoint = startPoint + ep;
|
||||
startControlPoint = startPoint + cp1;
|
||||
endControlPoint = startControlPoint + cp2;
|
||||
}
|
||||
|
||||
public override void stepInterval(float dt)
|
||||
{
|
||||
float t = timer / duration;
|
||||
Vector3 newPosition = (((-startPoint
|
||||
+ 3 * (startControlPoint - endControlPoint) + endPoint) * t
|
||||
+ (3 * (startPoint + endControlPoint) - 6 * startControlPoint)) * t +
|
||||
3 * (startControlPoint - startPoint)) * t + startPoint;
|
||||
transform.position = newPosition;
|
||||
}
|
||||
}
|
||||
33
src/ActionsInterval/ActionBlink.cs
Normal file
33
src/ActionsInterval/ActionBlink.cs
Normal file
@@ -0,0 +1,33 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
class ActionBlink : ActionRepeat
|
||||
{
|
||||
protected bool randomDelay;
|
||||
protected ActionDelay delay = new ActionDelay(0);
|
||||
protected Action[] blinkSeq;
|
||||
|
||||
public ActionBlink(int tgtBlinks, float tgtDuration)
|
||||
: base(null, tgtBlinks)
|
||||
{
|
||||
duration = tgtDuration;
|
||||
count = (tgtBlinks) * 2;
|
||||
blinkSeq = new Action[]
|
||||
{
|
||||
new ActionToggleVisibility(),
|
||||
new ActionDelay(tgtDuration / tgtBlinks)
|
||||
};
|
||||
action = new ActionSequence(blinkSeq);
|
||||
}
|
||||
|
||||
public override Action clone()
|
||||
{
|
||||
return new ActionBlink(count / 2 - 1, duration);
|
||||
}
|
||||
|
||||
public override Action reverse()
|
||||
{
|
||||
return new ActionBlink(count / 2 - 1, duration);
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,7 @@ using UnityEngine;
|
||||
|
||||
class ActionFadeBy : ActionInterval
|
||||
{
|
||||
public float delta;
|
||||
protected float delta;
|
||||
|
||||
public ActionFadeBy(float tgtDelta, float tgtDuration)
|
||||
: base(tgtDuration)
|
||||
@@ -25,8 +25,8 @@ class ActionFadeBy : ActionInterval
|
||||
public override void stepInterval(float dt)
|
||||
{
|
||||
float d = dt / duration;
|
||||
Color tgtColor = target.gameObject.renderer.material.color;
|
||||
Color tgtColor = renderer.material.color;
|
||||
tgtColor[3] += delta * d;
|
||||
target.gameObject.renderer.material.color = tgtColor;
|
||||
renderer.material.color = tgtColor;
|
||||
}
|
||||
}
|
||||
@@ -4,8 +4,8 @@ using UnityEngine;
|
||||
|
||||
class ActionFadeTo : ActionInterval
|
||||
{
|
||||
public float value;
|
||||
public float delta;
|
||||
protected float value;
|
||||
protected float delta;
|
||||
|
||||
public ActionFadeTo(float tgtValue, float tgtDuration)
|
||||
: base(tgtDuration)
|
||||
@@ -21,14 +21,14 @@ class ActionFadeTo : ActionInterval
|
||||
public override void start()
|
||||
{
|
||||
base.start();
|
||||
delta = value - target.gameObject.renderer.material.color.a;
|
||||
delta = value - renderer.material.color.a;
|
||||
}
|
||||
|
||||
public override void stepInterval(float dt)
|
||||
{
|
||||
float d = dt / duration;
|
||||
Color tgtColor = target.gameObject.renderer.material.color;
|
||||
Color tgtColor = renderer.material.color;
|
||||
tgtColor[3] += delta * d;
|
||||
target.gameObject.renderer.material.color = tgtColor;
|
||||
renderer.material.color = tgtColor;
|
||||
}
|
||||
}
|
||||
43
src/ActionsInterval/ActionJumpBy.cs
Normal file
43
src/ActionsInterval/ActionJumpBy.cs
Normal file
@@ -0,0 +1,43 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
class ActionJumpBy : ActionSequence
|
||||
{
|
||||
protected Vector3 point;
|
||||
float height;
|
||||
int jumps;
|
||||
|
||||
public ActionJumpBy(Vector3 tgtPoint, float tgtHeight, int tgtJumps, float tgtDuration)
|
||||
: base(new Action[tgtJumps])
|
||||
{
|
||||
point = tgtPoint;
|
||||
jumps = tgtJumps;
|
||||
height = tgtHeight;
|
||||
duration = tgtDuration;
|
||||
}
|
||||
|
||||
public override Action clone()
|
||||
{
|
||||
return new ActionJumpBy(point, height, jumps, duration);
|
||||
}
|
||||
|
||||
public override Action reverse()
|
||||
{
|
||||
return new ActionJumpBy(-point, height, jumps, duration);
|
||||
}
|
||||
|
||||
public override void start()
|
||||
{
|
||||
float coeff = 1F / jumps;
|
||||
Vector3 end = point * coeff;
|
||||
Vector3 cp1 = Vector3.up * height;
|
||||
Vector3 cp2 = end + cp1;
|
||||
ActionBezierRel singleJump = new ActionBezierRel(cp1, cp2, end, duration * coeff);
|
||||
for (int i = 0; i < jumps; i++)
|
||||
{
|
||||
actions[i] = singleJump;
|
||||
}
|
||||
base.start();
|
||||
}
|
||||
}
|
||||
39
src/ActionsInterval/ActionJumpTo.cs
Normal file
39
src/ActionsInterval/ActionJumpTo.cs
Normal file
@@ -0,0 +1,39 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
class ActionJumpTo : ActionSequence
|
||||
{
|
||||
protected Vector3 point;
|
||||
float height;
|
||||
int jumps;
|
||||
|
||||
public ActionJumpTo(Vector3 tgtPoint, float tgtHeight, int tgtJumps, float tgtDuration)
|
||||
: base(new Action[tgtJumps])
|
||||
{
|
||||
point = tgtPoint;
|
||||
jumps = tgtJumps;
|
||||
height = tgtHeight;
|
||||
duration = tgtDuration;
|
||||
}
|
||||
|
||||
public override Action clone()
|
||||
{
|
||||
return new ActionJumpTo(point, height, jumps, duration);
|
||||
}
|
||||
|
||||
public override void start()
|
||||
{
|
||||
float coeff = 1F / jumps;
|
||||
Vector3 start = target.gameObject.transform.position;
|
||||
Vector3 end = (point - start) * coeff;
|
||||
Vector3 cp1 = Vector3.up * height;
|
||||
Vector3 cp2 = end + cp1;
|
||||
ActionBezierRel singleJump = new ActionBezierRel(cp1, cp2, end, duration * coeff);
|
||||
for (int i = 0; i < jumps; i++)
|
||||
{
|
||||
actions[i] = singleJump;
|
||||
}
|
||||
base.start();
|
||||
}
|
||||
}
|
||||
@@ -26,6 +26,6 @@ class ActionMoveBy : ActionInterval
|
||||
{
|
||||
float d = dt / duration;
|
||||
Vector3 tgt = delta * d;
|
||||
target.gameObject.transform.Translate(tgt, Space.World);
|
||||
transform.Translate(tgt, Space.World);
|
||||
}
|
||||
}
|
||||
@@ -22,13 +22,13 @@ class ActionMoveTo : ActionInterval
|
||||
public override void start()
|
||||
{
|
||||
base.start();
|
||||
path = value - target.gameObject.transform.position;
|
||||
path = value - transform.position;
|
||||
}
|
||||
|
||||
public override void stepInterval(float dt)
|
||||
{
|
||||
float d = dt / duration;
|
||||
Vector3 tgt = path * d;
|
||||
target.gameObject.transform.Translate(tgt, Space.World);
|
||||
transform.Translate(tgt, Space.World);
|
||||
}
|
||||
}
|
||||
@@ -26,8 +26,8 @@ class ActionRotateBy : ActionInterval
|
||||
{
|
||||
float d = dt / duration;
|
||||
Vector3 tgt = delta * d;
|
||||
target.gameObject.transform.Rotate(Vector3.up, tgt.y);
|
||||
target.gameObject.transform.Rotate(Vector3.right, tgt.x);
|
||||
target.gameObject.transform.Rotate(Vector3.forward, tgt.z);
|
||||
transform.Rotate(Vector3.up, tgt.y);
|
||||
transform.Rotate(Vector3.right, tgt.x);
|
||||
transform.Rotate(Vector3.forward, tgt.z);
|
||||
}
|
||||
}
|
||||
@@ -25,7 +25,7 @@ class ActionRotateTo : ActionInterval
|
||||
for (int i = 0; i < 3; i++)
|
||||
{
|
||||
float t = value[i];
|
||||
float f = target.gameObject.transform.rotation.eulerAngles[i];
|
||||
float f = transform.rotation.eulerAngles[i];
|
||||
if (Math.Abs(t - f) < Math.Abs(t + 360 - f))
|
||||
path[i] = t - f;
|
||||
else
|
||||
@@ -36,8 +36,8 @@ class ActionRotateTo : ActionInterval
|
||||
{
|
||||
float d = dt / duration;
|
||||
Vector3 tgt = path * d;
|
||||
target.gameObject.transform.Rotate(Vector3.up, tgt.y);
|
||||
target.gameObject.transform.Rotate(Vector3.right, tgt.x);
|
||||
target.gameObject.transform.Rotate(Vector3.forward, tgt.z);
|
||||
transform.Rotate(Vector3.up, tgt.y);
|
||||
transform.Rotate(Vector3.right, tgt.x);
|
||||
transform.Rotate(Vector3.forward, tgt.z);
|
||||
}
|
||||
}
|
||||
@@ -26,18 +26,18 @@ class ActionScaleBy : ActionInterval
|
||||
public override void start()
|
||||
{
|
||||
base.start();
|
||||
Vector3 scale = target.gameObject.transform.localScale;
|
||||
Vector3 scale = transform.localScale;
|
||||
scale.x *= delta.x;
|
||||
scale.y *= delta.y;
|
||||
scale.z *= delta.z;
|
||||
path = scale - target.gameObject.transform.localScale;
|
||||
path = scale - transform.localScale;
|
||||
}
|
||||
|
||||
public override void stepInterval(float dt)
|
||||
{
|
||||
float d = dt / duration;
|
||||
Vector3 tgt = path * d;
|
||||
target.gameObject.transform.localScale += tgt;
|
||||
transform.localScale += tgt;
|
||||
}
|
||||
|
||||
public override void stop()
|
||||
|
||||
@@ -21,14 +21,14 @@ class ActionScaleTo : ActionInterval
|
||||
public override void start()
|
||||
{
|
||||
base.start();
|
||||
path = value - target.gameObject.transform.localScale;
|
||||
path = value - transform.localScale;
|
||||
}
|
||||
|
||||
public override void stepInterval(float dt)
|
||||
{
|
||||
float d = dt / duration;
|
||||
Vector3 tgt = path * d;
|
||||
target.gameObject.transform.localScale += tgt;
|
||||
transform.localScale += tgt;
|
||||
}
|
||||
|
||||
public override void stop()
|
||||
|
||||
@@ -4,7 +4,7 @@ using UnityEngine;
|
||||
|
||||
class ActionTintBy : ActionInterval
|
||||
{
|
||||
public Vector4 color;
|
||||
protected Vector4 color;
|
||||
protected const float coeff = 1F / 255F;
|
||||
|
||||
public ActionTintBy(Vector4 tgtColor, float tgtDuration)
|
||||
@@ -32,11 +32,11 @@ class ActionTintBy : ActionInterval
|
||||
{
|
||||
float d = dt / duration;
|
||||
Vector4 tgt = color * d;
|
||||
Color tgtColor = target.gameObject.renderer.material.color;
|
||||
Color tgtColor = renderer.material.color;
|
||||
tgtColor[0] += tgt[0];
|
||||
tgtColor[1] += tgt[1];
|
||||
tgtColor[2] += tgt[2];
|
||||
tgtColor[3] += tgt[3];
|
||||
target.gameObject.renderer.material.color = tgtColor;
|
||||
renderer.material.color = tgtColor;
|
||||
}
|
||||
}
|
||||
@@ -4,8 +4,8 @@ using UnityEngine;
|
||||
|
||||
class ActionTintTo : ActionInterval
|
||||
{
|
||||
public Vector4 color;
|
||||
public Vector4 path;
|
||||
protected Vector4 color;
|
||||
protected Vector4 path;
|
||||
protected const float coeff = 1F / 255F;
|
||||
|
||||
public ActionTintTo(Vector4 tgtColor, float tgtDuration)
|
||||
@@ -28,7 +28,7 @@ class ActionTintTo : ActionInterval
|
||||
public override void start()
|
||||
{
|
||||
base.start();
|
||||
Color tgtColor = target.gameObject.renderer.material.color;
|
||||
Color tgtColor = renderer.material.color;
|
||||
path[0] = color[0] - tgtColor[0];
|
||||
path[1] = color[1] - tgtColor[1];
|
||||
path[2] = color[2] - tgtColor[2];
|
||||
@@ -39,11 +39,11 @@ class ActionTintTo : ActionInterval
|
||||
{
|
||||
float d = dt / duration;
|
||||
Vector4 tgt = path * d;
|
||||
Color tgtColor = target.gameObject.renderer.material.color;
|
||||
Color tgtColor = renderer.material.color;
|
||||
tgtColor[0] += tgt[0];
|
||||
tgtColor[1] += tgt[1];
|
||||
tgtColor[2] += tgt[2];
|
||||
tgtColor[3] += tgt[3];
|
||||
target.gameObject.renderer.material.color = tgtColor;
|
||||
renderer.material.color = tgtColor;
|
||||
}
|
||||
}
|
||||
@@ -9,8 +9,7 @@ public class SampleActions : MonoBehaviour
|
||||
{
|
||||
new ActionFadeIn(2),
|
||||
new ActionParallel(new Action[] {
|
||||
new ActionMoveBy(new Vector3(10, 0, 0), 1),
|
||||
new ActionMoveBy(new Vector3(0, 10, 0), 1),
|
||||
new ActionMoveBy(new Vector3(10, 10, 0), 1),
|
||||
new ActionRotateBy(new Vector3(90, 0, 0), 1),
|
||||
new ActionTintBy(new Vector4(-50, 50, -150), 1)
|
||||
}),
|
||||
@@ -24,11 +23,14 @@ public class SampleActions : MonoBehaviour
|
||||
new ActionDelay(0F, 0.2F),
|
||||
}), 5),
|
||||
new ActionDelay(1),
|
||||
new ActionMoveBy(new Vector3(-10, 0, 0), 1),
|
||||
new ActionJumpBy(new Vector3(-10, 0, 0), 1, 4, 1),
|
||||
new ActionJumpTo(new Vector3(10, 10, 10), 1, 3, 1),
|
||||
new ActionJumpBy(new Vector3(-10, 0, 0), 1, 2, 1),
|
||||
new ActionDelay(0.5F),
|
||||
new ActionMoveTo(new Vector3(0, 0, 10), 1),
|
||||
new ActionBezierRel(new Vector3 (5, 0, 0), new Vector3(5, -10, 0), new Vector3 (0, -10, 0), 2),
|
||||
new ActionScaleTo(new Vector3(2, 2, 2), 1),
|
||||
new ActionRotateTo(new Vector3(0, 0, 0), 1),
|
||||
new ActionBlink(2, 1),
|
||||
new ActionFadeOut(2),
|
||||
new ActionSetTint(new Vector4(67, 105, 181))
|
||||
}), 5);
|
||||
|
||||
@@ -9,6 +9,12 @@ public class SkewModifier : MonoBehaviour
|
||||
protected Vector3 angles1prev = Vector3.zero;
|
||||
protected Vector3 angles2prev = Vector3.zero;
|
||||
protected const float coeff = Mathf.PI/180;
|
||||
protected Mesh mesh;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
mesh = gameObject.GetComponent<MeshFilter>().mesh;
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
@@ -30,7 +36,7 @@ public class SkewModifier : MonoBehaviour
|
||||
m[2, 0] = Mathf.Tan(angles1.z * coeff) - Mathf.Tan(angles1prev.z * coeff); // skewing in zx
|
||||
m[2, 1] = Mathf.Tan(angles2.z * coeff) - Mathf.Tan(angles2prev.z * coeff); // skewing in zy
|
||||
|
||||
Vector3[] verts = gameObject.GetComponent<MeshFilter>().mesh.vertices;
|
||||
Vector3[] verts = mesh.vertices;
|
||||
int i = 0;
|
||||
while (i < verts.Length)
|
||||
{
|
||||
@@ -38,7 +44,7 @@ public class SkewModifier : MonoBehaviour
|
||||
i++;
|
||||
}
|
||||
|
||||
gameObject.GetComponent<MeshFilter>().mesh.vertices = verts;
|
||||
mesh.vertices = verts;
|
||||
angles1prev = angles1;
|
||||
angles2prev = angles2;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user