Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/baba-s/UniCommandController
A simple library that can control commands to implement event scripts.
https://github.com/baba-s/UniCommandController
kogane-unity-lib unity unity-scripts unity3d
Last synced: about 2 months ago
JSON representation
A simple library that can control commands to implement event scripts.
- Host: GitHub
- URL: https://github.com/baba-s/UniCommandController
- Owner: baba-s
- License: mit
- Created: 2018-02-22T12:48:25.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2020-04-19T09:07:47.000Z (over 4 years ago)
- Last Synced: 2024-05-13T20:58:53.124Z (8 months ago)
- Topics: kogane-unity-lib, unity, unity-scripts, unity3d
- Language: C#
- Homepage: http://baba-s.hatenablog.com/entry/2018/02/28/090000
- Size: 48.8 KB
- Stars: 15
- Watchers: 7
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
- awesome-unity-open-source-on-github - unity-command-controller - A simple library that can control commands to implement event scripts (Script Utility)
README
# UniCommandController
A simple library that can control commands to implement event scripts.
# Usage
```cs
using System;
using UnityCommandController;
using UnityEngine;public class Example : MonoBehaviour
{
private CommandController m_controller;private void Awake()
{
// Type list of commands to use.
var commandTypes = new []
{
typeof( LogCommand ),
typeof( CreateCommand ),
typeof( SetPositionCommand ),
typeof( MoveCommand ),
typeof( JumpCommand ),
typeof( WaitCommand ),
typeof( ClickCommand ),
};// Create instances to control commands.
m_controller = new CommandController( commandTypes );// Create a list of commands.
var commands = new[]
{
"LogCommand|Pikachu",
"LogCommand|Raichu",
"CreateCommand|cube|0|1|2",
"SetPositionCommand|1|2|3",
"MoveCommand|-1|0|1|1",
"JumpCommand|6",
"LogCommand|Ignored here.",
"LogCommand|Jumped here.",
"WaitCommand|1",
"ClickCommand",
};// Objects and parameters controlled by command.
GameObject cube = null;
var startPos = Vector3.zero;
var endPos = Vector3.zero;// Set command event.
CreateCommand.OnCreate += go => cube = go;
SetPositionCommand.OnSetPosition += pos => cube.transform.localPosition = pos;
MoveCommand.OnMoveStart += pos =>
{
startPos = cube.transform.localPosition;
endPos = pos;
};
MoveCommand.OnMove += amount =>
{
cube.transform.localPosition = Vector3.Lerp( startPos, endPos, amount );
};
MoveCommand.OnMoveEnd += () => cube.transform.localPosition = endPos;
JumpCommand.OnJump += index => m_controller.JumpToIndex( index );// Called when all commands are finished.
m_controller.OnEnd += () => print( "Finished." );// Command start.
m_controller.Start( commands );
}private void Update()
{
// Command update.
m_controller.Update();
}
}public class LogCommand : CommandBase
{
private string m_message;public LogCommand( CommandArugments args )
{
m_message = args[ 1 ];
}protected override void DoStart()
{
Debug.Log( m_message );
}
}public class CreateCommand : CommandBase
{
private string m_name;
private Vector3 m_pos;public static event Action OnCreate = delegate { };
public CreateCommand( CommandArugments args )
{
m_name = args[ 1 ];
m_pos = new Vector3
(
args.ToFloat( 2 ),
args.ToFloat( 3 ),
args.ToFloat( 4 )
);
}protected override void DoStart()
{
var go = GameObject.CreatePrimitive( PrimitiveType.Cube );
go.name = m_name;
go.transform.localPosition = m_pos;
OnCreate( go );
Debug.Log( "Object creation complete." );
}
}public class SetPositionCommand : CommandBase
{
private Vector3 m_pos;public static event Action OnSetPosition = delegate { };
public SetPositionCommand( CommandArugments args )
{
m_pos = new Vector3
(
args.ToFloat( 1 ),
args.ToFloat( 2 ),
args.ToFloat( 3 )
);
}protected override void DoStart()
{
OnSetPosition( m_pos );
Debug.Log( "Object position setting complete." );
}
}public class MoveCommand : CommandBase
{
private Vector3 m_pos;
private float m_duration;
private float m_elapsedTime;public override bool IsEnd { get { return m_duration <= m_elapsedTime; } }
public static event Action OnMoveStart = delegate { };
public static event Action OnMove = delegate { };
public static event Action OnMoveEnd = delegate { };public MoveCommand( CommandArugments args )
{
m_pos = new Vector3
(
args.ToFloat( 1 ),
args.ToFloat( 2 ),
args.ToFloat( 3 )
);
m_duration = args.ToFloat( 4 );
}protected override void DoStart()
{
OnMoveStart( m_pos );
Debug.Log( "Object movement start." );
}protected override void DoUpdate()
{
m_elapsedTime += Time.deltaTime;
OnMove( m_elapsedTime / m_duration );
}protected override void DoDispose()
{
OnMoveEnd();
Debug.Log( "End of object movement." );
}
}public class JumpCommand : CommandBase
{
private int m_index;public static event Action OnJump = delegate { };
public JumpCommand( CommandArugments args )
{
m_index = args.ToInt( 1 );
}protected override void DoStart()
{
OnJump( m_index );
}
}public class WaitCommand : CommandBase
{
private float m_time;
private float m_elapsedTime;public override bool IsEnd { get { return m_time <= m_elapsedTime; } }
public WaitCommand( CommandArugments args )
{
m_time = args.ToFloat( 1 );
}protected override void DoStart()
{
Debug.Log( "Standby start." );
}protected override void DoUpdate()
{
m_elapsedTime += Time.deltaTime;
}protected override void DoDispose()
{
Debug.Log( "Wait end." );
}
}public class ClickCommand : CommandBase
{
public override bool IsEnd { get { return Input.GetMouseButtonDown( 0 ); } }public ClickCommand( CommandArugments args ) { }
protected override void DoStart()
{
Debug.Log( "Wait for input." );
}protected override void DoDispose()
{
Debug.Log( "Input confirmation." );
}
}
```