Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -193,3 +193,21 @@ FakesAssemblies/

# Visual Studio 6 workspace options file
*.opt

Temp/

Library/

Obj/

*.csproj

*.unityproj

*.sln

*.user

*.userprefs

*.DS_store
Binary file added Lab-02 Report.docx
Binary file not shown.
Binary file added Lab-02-ASeba/Assets/Controller.controller
Binary file not shown.
8 changes: 8 additions & 0 deletions Lab-02-ASeba/Assets/Controller.controller.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added Lab-02-ASeba/Assets/Cube.prefab
Binary file not shown.
8 changes: 8 additions & 0 deletions Lab-02-ASeba/Assets/Cube.prefab.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added Lab-02-ASeba/Assets/DarkElf_0.prefab
Binary file not shown.
8 changes: 8 additions & 0 deletions Lab-02-ASeba/Assets/DarkElf_0.prefab.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions Lab-02-ASeba/Assets/Editor.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

220 changes: 220 additions & 0 deletions Lab-02-ASeba/Assets/Editor/MakeAnimations.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
using UnityEngine;
using System.Collections;
using UnityEditor;

public class MakeAnimations : EditorWindow {

//This is static because the menu function is also static*
public static Object selectedObject;

int numAnimations;
string controllerName;
string[] animationNames = new string[100];
float[] clipFrameRate = new float[100];
float[] clipTimeBetween = new float[100];
int[] startFrames = new int[100];
int[] endFrames = new int[100];
bool[] pingPong = new bool[100];
bool[] loop = new bool[100];

/// <summary>
/// store the selected objects and open a popup window
/// </summary>
[MenuItem("Project Tools/2D Animations")]
static void Init()
{
selectedObject = Selection.activeObject;

if(selectedObject == null)
{
return;
}

MakeAnimations window = (MakeAnimations)EditorWindow.GetWindow(typeof(MakeAnimations));

window.Show();
}

void OnGUI()
{
if(selectedObject != null)
{
EditorGUILayout.LabelField("Animations for " + selectedObject.name);
EditorGUILayout.Separator();

controllerName = EditorGUILayout.TextField("Controller Name", controllerName);

numAnimations = EditorGUILayout.IntField("How many animations?", numAnimations);

for (int i = 0; i < numAnimations; i++)
{
//Get the animation name
animationNames[i] = EditorGUILayout.TextField("Animation Name", animationNames[i]);

//Start a section where the items are displayed horizontally
EditorGUILayout.BeginHorizontal();

startFrames[i] = EditorGUILayout.IntField("Start Frame", startFrames[i]);

//get end frame
endFrames[i] = EditorGUILayout.IntField("End Frame", endFrames[i]);

EditorGUILayout.EndHorizontal();

//Start a section where the following items will be displayed horizontally instead of vertically
EditorGUILayout.BeginHorizontal();
//Determine the frame rate for the animation
clipFrameRate[i] = EditorGUILayout.FloatField("Frame Rate", clipFrameRate[i]);
//Determine the space between each keyframe
clipTimeBetween[i] = EditorGUILayout.FloatField("Frame Spacing", clipTimeBetween[i]);
//End the section where the previous items are displayed horitontally instead of vertically
EditorGUILayout.EndHorizontal();

//Start a section where the following items will be displayed horizontally instead of vertically
EditorGUILayout.BeginHorizontal();
//Create a checkbox to determine if this animation should loop
loop[i] = EditorGUILayout.Toggle("Loop", loop[i]);
//Create a checkbox to determine if this animation should pingpong
pingPong[i] = EditorGUILayout.Toggle("Ping Pong", pingPong[i]);
//End the section where the previous items are displayed horitontally instead of vertically
EditorGUILayout.EndHorizontal();

EditorGUILayout.Separator();

}

if (GUILayout.Button("Create"))
{
UnityEditor.Animations.AnimatorController controller = UnityEditor.Animations.AnimatorController.CreateAnimatorControllerAtPath(("Assets/" + controllerName + ".controller"));

for(int i = 0; i< numAnimations; i++)
{
AnimationClip tempClip = CreateClip(selectedObject, animationNames[i], startFrames[i], endFrames[i], clipFrameRate[i], clipTimeBetween[i], pingPong[i]);

if (loop[i])
{
//set the loop on the clip to true
AnimationClipSettings settings = AnimationUtility.GetAnimationClipSettings(tempClip);
settings.loopTime = true;
settings.loopBlend = true;
AnimationUtility.SetAnimationClipSettings(tempClip, settings);
}

controller.AddMotion(tempClip);
}
}

}
}

void OnFocus()
{
if (EditorApplication.isPlayingOrWillChangePlaymode)
{
this.Close();
}
}

public AnimationClip CreateClip(
Object obj,
string clipName,
int startFrame,
int endFrame,
float frameRate,
float timeBetween,
bool pingPong)
{
string path = AssetDatabase.GetAssetPath(obj);

Object[] sprites = AssetDatabase.LoadAllAssetsAtPath(path);

//check end frame
if (endFrame < sprites.Length)
{

//Determine how many frames, and the length of each frame
int frameCount = endFrame - startFrame + 1;

float frameLength = 1f / timeBetween;

//Create a new (empty animation clip
AnimationClip clip = new AnimationClip();

clip.frameRate = frameRate;

EditorCurveBinding curveBinding = new EditorCurveBinding();

//Assing it to change the sprite renderer

curveBinding.type = typeof(SpriteRenderer);
//assign it to the sprite renderer
curveBinding.propertyName = "m_Sprite";

//Create a container for all of the keyframes
ObjectReferenceKeyframe[] keyFrames;

//Determine how many frames if ping ponging

if (!pingPong)
{
keyFrames = new ObjectReferenceKeyframe[frameCount + 1];
}
else
{
keyFrames = new ObjectReferenceKeyframe[frameCount * 2 + 1];
}

//frame counter
int frameNumber = 0;

//loop from start to end
for (int i = startFrame; i < endFrame + 1; i++, frameNumber++)
{
//create empty keyframe
ObjectReferenceKeyframe tempKeyFrame = new ObjectReferenceKeyframe();
//assign it a time to appear in the anim
tempKeyFrame.time = frameNumber * frameLength;
//assign it a sprite
tempKeyFrame.value = sprites[i];
//place it into the container for all the keyframes
keyFrames[frameNumber] = tempKeyFrame;
}

//If we are pinponging this aimation
if (pingPong)
{
//create keyframes starting at the end and going backwards
//continue to keep track of the frame number
for (int i = endFrame; i >= startFrame; i--, frameNumber++)
{
ObjectReferenceKeyframe tempKeyFrame = new ObjectReferenceKeyframe();
tempKeyFrame.time = frameNumber * frameLength;
tempKeyFrame.value = sprites[i];
keyFrames[frameNumber] = tempKeyFrame;
}
}
//Create the last sprite to stop it from switching quickly from the last frame to the first one
ObjectReferenceKeyframe lastSprite = new ObjectReferenceKeyframe();
lastSprite.time = frameNumber * frameLength;
lastSprite.value = sprites[startFrame];
keyFrames[frameNumber] = lastSprite;

//Assign the name
clip.name = clipName;

//apply the curve
AnimationUtility.SetObjectReferenceCurve(clip, curveBinding, keyFrames);

//create the clip
AssetDatabase.CreateAsset(clip, ("Assets/" + clipName + ".anim"));

//return the clip
return clip;
}
else
{
Debug.Log("endFrame is greater than amount of slices the sprite has.");
return null;
}
}
}
12 changes: 12 additions & 0 deletions Lab-02-ASeba/Assets/Editor/MakeAnimations.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 49 additions & 0 deletions Lab-02-ASeba/Assets/Editor/ScriptToolMakePrefab.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using UnityEngine;
using System.Collections;
using UnityEditor;

public class ScriptToolMakePrefab : MonoBehaviour {

[MenuItem("Project Tools/Create Prefab")]
public static void CreatePrefab()
{
GameObject[] selectedObjects = Selection.gameObjects;

foreach (GameObject go in selectedObjects)
{
string name = go.name;
string assetPath = "Assets/" + name + ".prefab";


if(AssetDatabase.LoadAssetAtPath(assetPath, typeof(GameObject)))
{
Debug.Log("Asset exists!");
if(EditorUtility.DisplayDialog("Caution", "Prefab already exists. " +
"Do you want to overwrite?", "Yes", "No"))
{
Debug.Log("Overwriting!");
CreateNew(go, assetPath);
}
}
else
{
CreateNew(go, assetPath);
}

//Debug.Log("Name: " + go.name + "Path: " + assetPath);
}


}

public static void CreateNew(GameObject obj, string location)
{
Object prefab = PrefabUtility.CreateEmptyPrefab(location);
PrefabUtility.ReplacePrefab(obj, prefab);
AssetDatabase.Refresh();

DestroyImmediate(obj);

GameObject clone = PrefabUtility.InstantiatePrefab(prefab) as GameObject;
}
}
12 changes: 12 additions & 0 deletions Lab-02-ASeba/Assets/Editor/ScriptToolMakePrefab.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added Lab-02-ASeba/Assets/Foward.anim
Binary file not shown.
8 changes: 8 additions & 0 deletions Lab-02-ASeba/Assets/Foward.anim.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added Lab-02-ASeba/Assets/Left.anim
Binary file not shown.
8 changes: 8 additions & 0 deletions Lab-02-ASeba/Assets/Left.anim.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added Lab-02-ASeba/Assets/Right.anim
Binary file not shown.
8 changes: 8 additions & 0 deletions Lab-02-ASeba/Assets/Right.anim.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions Lab-02-ASeba/Assets/Sprite.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added Lab-02-ASeba/Assets/Sprite/DarkElf.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading