Browse Source

Optimization: caching unity components

new actions:
ActionBezierRel.cs
ActionBlink.cs
ActionJumpBy.cs
ActionJumpTo.cs
master
parent
commit
1d69dfd8f3
25 changed files with 234 additions and 61 deletions
  1. +5
    -5
      README.md
  2. +4
    -0
      src/ActionsBase/Action.cs
  3. +1
    -1
      src/ActionsInstant/ActionHide.cs
  4. +1
    -1
      src/ActionsInstant/ActionSetPlace.cs
  5. +4
    -4
      src/ActionsInstant/ActionSetRotation.cs
  6. +1
    -1
      src/ActionsInstant/ActionSetTint.cs
  7. +1
    -1
      src/ActionsInstant/ActionShow.cs
  8. +1
    -1
      src/ActionsInstant/ActionToggleVisibility.cs
  9. +5
    -10
      src/ActionsInterval/ActionBezierAbs.cs
  10. +51
    -0
      src/ActionsInterval/ActionBezierRel.cs
  11. +33
    -0
      src/ActionsInterval/ActionBlink.cs
  12. +3
    -3
      src/ActionsInterval/ActionFadeBy.cs
  13. +5
    -5
      src/ActionsInterval/ActionFadeTo.cs
  14. +43
    -0
      src/ActionsInterval/ActionJumpBy.cs
  15. +39
    -0
      src/ActionsInterval/ActionJumpTo.cs
  16. +1
    -1
      src/ActionsInterval/ActionMoveBy.cs
  17. +2
    -2
      src/ActionsInterval/ActionMoveTo.cs
  18. +3
    -3
      src/ActionsInterval/ActionRotateBy.cs
  19. +4
    -4
      src/ActionsInterval/ActionRotateTo.cs
  20. +3
    -3
      src/ActionsInterval/ActionScaleBy.cs
  21. +2
    -2
      src/ActionsInterval/ActionScaleTo.cs
  22. +3
    -3
      src/ActionsInterval/ActionTintBy.cs
  23. +5
    -5
      src/ActionsInterval/ActionTintTo.cs
  24. +6
    -4
      src/Examples/SampleActions.cs
  25. +8
    -2
      src/UnityComponents/SkewModifier.cs

+ 5
- 5
README.md View File

@@ -17,7 +17,7 @@ You can also subclass the Actor and add all the actions you want to the Start()

### Included actions (ready and WIP)
All actions are designed to word in 3D scene (i.e. using Vector3 for movement, rotation and scaling).
You can use it in 2D with appropriate vectors, but i'm going to add support for 2D actions soon.
You can use it in 2D with apropriate vectors, but i'm going to add support for 2D actions soon.

Base actions
- [x] Sequence
@@ -39,11 +39,11 @@ Interval actions
- [x] FadeIn *if you want to manipulate alpha, your material must support transparency*
- [x] FadeTo *if you want to manipulate alpha, your material must support transparency*
- [x] FadeBy *if you want to manipulate alpha, your material must support transparency*
- [ ] JumpTo
- [ ] JumpBy
- [x] BezierAbs
- [ ] BezierRel
- [ ] Blink
- [x] BezierRel
- [x] JumpTo *uses Bezier action to move the object*
- [x] JumpBy *uses Bezier action to move the object*
- [x] Blink

Instant actions
- [x] Place *renamed to SetPlace*


+ 4
- 0
src/ActionsBase/Action.cs View File

@@ -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>


+ 1
- 1
src/ActionsInstant/ActionHide.cs View File

@@ -18,6 +18,6 @@ class ActionHide : Action
public override void start()
{
base.start();
target.gameObject.renderer.enabled = false;
renderer.enabled = false;
}
}

+ 1
- 1
src/ActionsInstant/ActionSetPlace.cs View File

@@ -20,6 +20,6 @@ class ActionSetPlace : Action
public override void start()
{
base.start();
target.gameObject.transform.position = place;
transform.position = place;
}
}

+ 4
- 4
src/ActionsInstant/ActionSetRotation.cs View File

@@ -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);
}
}

+ 1
- 1
src/ActionsInstant/ActionSetTint.cs View File

@@ -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]);
}
}

+ 1
- 1
src/ActionsInstant/ActionShow.cs View File

@@ -18,6 +18,6 @@ class ActionShow : Action
public override void start()
{
base.start();
target.gameObject.renderer.enabled = true;
renderer.enabled = true;
}
}

+ 1
- 1
src/ActionsInstant/ActionToggleVisibility.cs View File

@@ -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;
}
}

+ 5
- 10
src/ActionsInterval/ActionBezierAbs.cs View File

@@ -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
- 0
src/ActionsInterval/ActionBezierRel.cs View 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
- 0
src/ActionsInterval/ActionBlink.cs View 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);
}
}

+ 3
- 3
src/ActionsInterval/ActionFadeBy.cs View File

@@ -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;
}
}

+ 5
- 5
src/ActionsInterval/ActionFadeTo.cs View File

@@ -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
- 0
src/ActionsInterval/ActionJumpBy.cs View 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
- 0
src/ActionsInterval/ActionJumpTo.cs View 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();
}
}

+ 1
- 1
src/ActionsInterval/ActionMoveBy.cs View File

@@ -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);
}
}

+ 2
- 2
src/ActionsInterval/ActionMoveTo.cs View File

@@ -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);
}
}

+ 3
- 3
src/ActionsInterval/ActionRotateBy.cs View File

@@ -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);
}
}

+ 4
- 4
src/ActionsInterval/ActionRotateTo.cs View File

@@ -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);
}
}

+ 3
- 3
src/ActionsInterval/ActionScaleBy.cs View File

@@ -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()


+ 2
- 2
src/ActionsInterval/ActionScaleTo.cs View File

@@ -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()


+ 3
- 3
src/ActionsInterval/ActionTintBy.cs View File

@@ -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;
}
}

+ 5
- 5
src/ActionsInterval/ActionTintTo.cs View File

@@ -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;
}
}

+ 6
- 4
src/Examples/SampleActions.cs View File

@@ -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);


+ 8
- 2
src/UnityComponents/SkewModifier.cs View File

@@ -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;
}