Skip to content

Commit ae9af81

Browse files
authored
Merge pull request #16 from k-okawa/develop/add_transition_id_generator
Develop/add transition id generator
2 parents 065e87b + 852c7f7 commit ae9af81

File tree

10 files changed

+148
-11
lines changed

10 files changed

+148
-11
lines changed

Assets/Bg/UniTaskStateMachine/Editor/EditorWindow/SubWindow.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using UnityEditor;
2+
using UnityEngine;
3+
4+
namespace Bg.UniTaskStateMachine.Editor.SubWindow
5+
{
6+
public class CodeGenerationResultWindow : EditorWindow
7+
{
8+
private Vector2 _scroll;
9+
private string text;
10+
11+
public static void Open(string title, string code)
12+
{
13+
var window = GetWindow<CodeGenerationResultWindow>(title);
14+
window.text = code;
15+
}
16+
17+
void OnGUI()
18+
{
19+
EditorGUILayout.BeginVertical();
20+
21+
EditorGUILayout.BeginHorizontal();
22+
EditorGUILayout.LabelField("GenerationResult");
23+
if (GUILayout.Button("Copy"))
24+
{
25+
EditorGUIUtility.systemCopyBuffer = text;
26+
}
27+
EditorGUILayout.EndHorizontal();
28+
29+
_scroll = EditorGUILayout.BeginScrollView(_scroll);
30+
text = EditorGUILayout.TextArea(text, EditorStyles.textArea);
31+
EditorGUILayout.EndScrollView();
32+
33+
34+
EditorGUILayout.EndVertical();
35+
}
36+
}
37+
}

Assets/Bg/UniTaskStateMachine/Editor/EditorWindow/SubWindow/CodeGenerationResultWindow.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Bg/UniTaskStateMachine/Editor/EditorWindow/Toolbar.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public Toolbar(EditorWindow editorWindow)
1313
this.elements = new List<ToolbarElement>()
1414
{
1515
new StateMachineField(editorWindow),
16+
new TransitionIdConstantGenerator(editorWindow),
1617
new ZoomSlider(editorWindow)
1718
};
1819
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System.Text;
2+
using Bg.UniTaskStateMachine.Editor.SubWindow;
3+
using UnityEngine;
4+
5+
namespace Bg.UniTaskStateMachine.Editor
6+
{
7+
public class TransitionIdConstantGenerator : ToolbarElement
8+
{
9+
public TransitionIdConstantGenerator(EditorWindow window) : base(window)
10+
{
11+
12+
}
13+
14+
public override void OnGUI(Rect rect)
15+
{
16+
if (!Context.IsStateMachineLoaded)
17+
{
18+
return;
19+
}
20+
21+
if (GUI.Button(new Rect(270, rect.y + 1, 180, 16),
22+
new GUIContent("GenerateTransitionIdConst", "Generate transitionId constant code")))
23+
{
24+
var result = GenerateConst();
25+
CodeGenerationResultWindow.Open("TransitionIdConstant", result);
26+
}
27+
}
28+
29+
private string GenerateConst()
30+
{
31+
var transitions = EditorWindow.Context.Graph.Cache.Transitions;
32+
StringBuilder sb = new StringBuilder();
33+
sb.AppendLine($"public static class {EditorWindow.Context.StateMachine.name}Const");
34+
sb.AppendLine("{");
35+
foreach (var transition in transitions)
36+
{
37+
sb.AppendLine($" public static readonly string {transition.ID} = \"{transition.ID}\";");
38+
}
39+
sb.AppendLine("}");
40+
41+
return sb.ToString();
42+
}
43+
}
44+
}

Assets/Bg/UniTaskStateMachine/Editor/EditorWindow/ToolbarElements/TransitionIdConstantGenerator.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Bg/UniTaskStateMachine/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "com.bg.unitaskstatemachine",
33
"displayName": "UniTaskStateMachine",
4-
"version": "1.0.4",
4+
"version": "1.1.0",
55
"unity": "2019.4",
66
"description": "UniTaskStateMachine",
77
"author": {

Assets/Bg/UniTaskStateMachineTest/Scenes/BasicTestScene.unity

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ RenderSettings:
4343
--- !u!157 &3
4444
LightmapSettings:
4545
m_ObjectHideFlags: 0
46-
serializedVersion: 11
46+
serializedVersion: 12
4747
m_GIWorkflowMode: 1
4848
m_GISettings:
4949
serializedVersion: 2
@@ -98,7 +98,8 @@ LightmapSettings:
9898
m_TrainingDataDestination: TrainingData
9999
m_LightProbeSampleCountMultiplier: 4
100100
m_LightingDataAsset: {fileID: 0}
101-
m_UseShadowmask: 1
101+
m_LightingSettings: {fileID: 4890085278179872738, guid: acfab88974ea1433caf0b01db05be33e,
102+
type: 2}
102103
--- !u!196 &4
103104
NavMeshSettings:
104105
serializedVersion: 2
@@ -118,6 +119,8 @@ NavMeshSettings:
118119
manualTileSize: 0
119120
tileSize: 256
120121
accuratePlacement: 0
122+
maxJobWorkers: 0
123+
preserveTilesOutsideBounds: 0
121124
debug:
122125
m_Flags: 0
123126
m_NavMeshData: {fileID: 0}
@@ -197,6 +200,7 @@ Light:
197200
m_UseColorTemperature: 0
198201
m_BoundingSphereOverride: {x: 0, y: 0, z: 0, w: 0}
199202
m_UseBoundingSphereOverride: 0
203+
m_UseViewFrustumForShadowCasterCull: 1
200204
m_ShadowRadius: 0
201205
m_ShadowAngle: 0
202206
--- !u!4 &705507995
@@ -258,13 +262,13 @@ MonoBehaviour:
258262
id: End
259263
stateComponent: {fileID: 854791544}
260264
transitions:
261-
- id: Transition
265+
- id: ToPlay
262266
description:
263267
originStateID: Start
264268
targetStateID: Play
265269
conditionMethodName: Bg.UniTaskStateMachine.Tests.BasicSceneTest.ConditionComponent/IsInitEnd
266270
isNegative: 0
267-
- id: Transition(3)
271+
- id: ToEnd
268272
description:
269273
originStateID: Play
270274
targetStateID: End
@@ -470,6 +474,7 @@ MeshRenderer:
470474
m_LightProbeUsage: 1
471475
m_ReflectionProbeUsage: 1
472476
m_RayTracingMode: 2
477+
m_RayTraceProcedural: 0
473478
m_RenderingLayerMask: 1
474479
m_RendererPriority: 0
475480
m_Materials:
@@ -494,6 +499,7 @@ MeshRenderer:
494499
m_SortingLayerID: 0
495500
m_SortingLayer: 0
496501
m_SortingOrder: 0
502+
m_AdditionalVertexStreams: {fileID: 0}
497503
--- !u!33 &1161249409
498504
MeshFilter:
499505
m_ObjectHideFlags: 0

README.ja.md

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ Editorツールも提供しているので、簡単にステートを組むこ
2929

3030
(スクリプト上で組むことも可能です)
3131

32-
![image](https://user-images.githubusercontent.com/49301086/137613577-d510a77c-0231-4e76-bf0f-a6f16a2ae506.png)
32+
![image](https://user-images.githubusercontent.com/49301086/183348165-6042c870-ac33-479a-b5fa-af210f345352.png)
3333

3434

3535
## インストール
@@ -64,6 +64,10 @@ https://github.com/k-okawa/UniTaskStateMachine.git?path=Assets/Bg/UniTaskStateMa
6464
openupm add com.bg.unitaskstatemachine
6565
```
6666

67+
### UnityPackage
68+
69+
[リリースページ](https://github.com/k-okawa/UniTaskStateMachine/releases)からダウンロード可能です。
70+
6771
## 使い方
6872
### 1.StateMachineBehaviour追加
6973
StateMachineBehaviourをAddComponentします。
@@ -75,7 +79,7 @@ StateMachineBehaviourをAddComponentします。
7579

7680
またはWindow/BG UniTaskStateMachine/StateMachineGraphでグラフエディタを開くことができます。
7781

78-
![image](https://user-images.githubusercontent.com/49301086/143770686-8efd36c8-35fc-40a5-a1ec-4862a2dda9e3.png)
82+
![image](https://user-images.githubusercontent.com/49301086/183349573-556b2bfb-968c-40ef-91b9-acde45bf5f65.png)
7983

8084
### 3.State追加方法
8185
#### 3-1.StateBehaviourを追加
@@ -85,6 +89,8 @@ StateMachineBehaviourがアタッチされているGameObjectにBaseStateCompone
8589

8690
※BaseStateComponentを直接AddComponentしないでください。
8791

92+
※同じStateComponentを同じゲームオブジェクトにAddComponentしないでください。
93+
8894
****
8995
```c#
9096
namespace Bg.UniTaskStateMachine.Tests.BasicSceneTest
@@ -115,7 +121,7 @@ namespace Bg.UniTaskStateMachine.Tests.BasicSceneTest
115121

116122
Graphエディタ上で右クリックし、CreateStateでState追加
117123

118-
![image](https://user-images.githubusercontent.com/49301086/143770989-faa1a688-2ecd-4407-87d7-3e9b2a4c570a.png)
124+
![image](https://user-images.githubusercontent.com/49301086/183352350-6de6bec5-b304-4a1a-b668-049c6841a9eb.png)
119125

120126
追加されているクラス名が表示され選択可能になります。
121127

@@ -148,6 +154,20 @@ Stateを右クリックし、Set as Entryを選択することで設定するこ
148154

149155
![image](https://user-images.githubusercontent.com/49301086/143771406-b0e40166-fd07-4091-8e98-a5cac1ba83f2.png)
150156

157+
### TriggerNextTransition
158+
159+
トランジションに条件を指定する以外にも、IDを指定してステートを遷移させることが可能です。
160+
161+
トランジションIDはC#のフィールド名で使用可能なアッパーキャメルケースを推奨します。
162+
163+
すべてのトランジションIDを決めた後、GenerateTransitionIdConstボタンを使用して定数のテンプレートコードを生成できます。
164+
165+
![image](https://user-images.githubusercontent.com/49301086/183354384-8c33ea1d-53a3-4bae-95f1-2011c0ddd660.png)
166+
167+
最後に、"StateMachine.TriggerNextTransition(string transitionId)"を呼び出すだけで好きなタイミングでステート遷移が可能です
168+
169+
引数に文字列を直接渡すことができますが、生成されたクラスの読み取り専用のフィールドで渡すことを推奨します。
170+
151171
## API Reference
152172
### StateMachine
153173
StateMachineBehaviourのStateMachineプロパティからアクセスできます。

README.md

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ In addition, the state transition method corresponds to the following.
2727

2828
This package also provide an Editor tool, so you can easily create states.
2929

30-
![image](https://user-images.githubusercontent.com/49301086/137613577-d510a77c-0231-4e76-bf0f-a6f16a2ae506.png)
30+
![image](https://user-images.githubusercontent.com/49301086/183348165-6042c870-ac33-479a-b5fa-af210f345352.png)
3131

3232

3333
## Installation
@@ -60,6 +60,10 @@ https://github.com/k-okawa/UniTaskStateMachine.git?path=Assets/Bg/UniTaskStateMa
6060
openupm add com.bg.unitaskstatemachine
6161
```
6262

63+
### UnityPackage
64+
65+
You can download unity package in [release page](https://github.com/k-okawa/UniTaskStateMachine/releases).
66+
6367
## How to use
6468
### 1.Add StateMachineBehaviour Component
6569

@@ -71,7 +75,7 @@ GraphEditorOpen of StateMachineBehaviour added in 1
7175

7276
Or you can open the graph editor with Window / BG UniTaskStateMachine / StateMachineGraph.
7377

74-
![image](https://user-images.githubusercontent.com/49301086/143770686-8efd36c8-35fc-40a5-a1ec-4862a2dda9e3.png)
78+
![image](https://user-images.githubusercontent.com/49301086/183349573-556b2bfb-968c-40ef-91b9-acde45bf5f65.png)
7579

7680
### 3.How to add state
7781
#### 3-1.Add StateBehaviour
@@ -83,6 +87,8 @@ Add a Component that inherits BaseStateComponent to the GameObject to which Stat
8387

8488
* Do not addBaseStateComponent directly.
8589

90+
* Do not add same state component
91+
8692
**Example**
8793
```c#
8894
namespace Bg.UniTaskStateMachine.Tests.BasicSceneTest
@@ -113,7 +119,7 @@ namespace Bg.UniTaskStateMachine.Tests.BasicSceneTest
113119

114120
Right-click on the Graph editor and select Create State to add State.
115121

116-
![image](https://user-images.githubusercontent.com/49301086/143770989-faa1a688-2ecd-4407-87d7-3e9b2a4c570a.png)
122+
![image](https://user-images.githubusercontent.com/49301086/183352350-6de6bec5-b304-4a1a-b668-049c6841a9eb.png)
117123

118124
The added class name will be displayed and can be selected.
119125

@@ -149,6 +155,20 @@ It can be set by right-clicking on the State and selecting Set as Entry.
149155

150156
![image](https://user-images.githubusercontent.com/49301086/143771406-b0e40166-fd07-4091-8e98-a5cac1ba83f2.png)
151157

158+
### TriggerNextTransition
159+
160+
It is possible to translate state manually by transition id without specify condition method.
161+
162+
Transition id is recommended upper camel case that can use in C# field name.
163+
164+
After define transition id, you can generate transition id constant template code by GenerateTransitionIdConst button.
165+
166+
![image](https://user-images.githubusercontent.com/49301086/183354384-8c33ea1d-53a3-4bae-95f1-2011c0ddd660.png)
167+
168+
Finally, call "StateMachine.TriggerNextTransition(string transitionId)" for manual state transition.
169+
170+
It is able to call with string direct writing, but recommend with readonly string field in the generated template class.
171+
152172
## API Reference
153173
### StateMachine
154174
It can be accessed from the StateMachine property of the StateMachineBehaviour.

0 commit comments

Comments
 (0)