📌 Unity 최종 프로젝트
📌 추가된 점
➔ Sound SFX Apply
- Player Attack(Punch, NearWeapon, Pistol, Rifle)
➔ Behavior Tree Editor View Build Setting
[CreateAssetMenu()]
public class BehaviorTree : ScriptableObject
{
public Node rootNode;
public E_NodeState treeState = E_NodeState.Running;
public List<Node> nodes = new List<Node>();
public DataContext dataContext = new DataContext();
public E_NodeState Update()
{
if (rootNode.state == E_NodeState.Running)
{
treeState = rootNode.Update();
}
return treeState;
}
public List<Node> GetChildren(Node parent)
{
List<Node> children = new List<Node>();
Root root = parent as Root;
if (root && root.child != null)
children.Add(root.child);
Decorator decorator = parent as Decorator;
if (decorator && decorator.child != null)
children.Add(decorator.child);
Composite composite = parent as Composite;
if (composite)
return composite.children;
return children;
}
public void Traverse(Node node, System.Action<Node> visitor)
{
if (node)
{
visitor.Invoke(node);
var children = GetChildren(node);
children.ForEach((n) => Traverse(n, visitor));
}
}
public BehaviorTree Clone()
{
BehaviorTree tree = Instantiate(this);
tree.rootNode = tree.rootNode.Clone();
tree.nodes = new List<Node>();
Traverse(tree.rootNode, (n) => { tree.nodes.Add(n); });
return tree;
}
public void Bind(BasicZombieData zombieData)
{
Traverse(rootNode, node =>
{
node.zombieData = zombieData;
node.dataContext = dataContext;
});
}
#region MyRegion
#if UNITY_EDITOR
public Node CreateNode(System.Type type)
{
Node node = ScriptableObject.CreateInstance(type) as Node;
if (node == null)
Debug.LogError("node CreateInstance failed");
node.name = type.Name;
node.guid = GUID.Generate().ToString();
Undo.RecordObject(this, "Behavior Tree (CreateNode)");
nodes.Add(node);
if (!Application.isPlaying)
AssetDatabase.AddObjectToAsset(node, this);
Undo.RegisterCreatedObjectUndo(node, "BBehavior Tree (CreateNode)");
AssetDatabase.SaveAssets();
return node;
}
public void DeleteNode(Node node)
{
Undo.RecordObject(this, "Behavior Tree (DeleteNode)");
nodes.Remove(node);
Undo.DestroyObjectImmediate(node);
AssetDatabase.SaveAssets();
}
public void AddChild(Node parent, Node child)
{
Root root = parent as Root;
if (root)
{
Undo.RecordObject(root, "Behavior Tree (AddChild)");
root.child = child;
EditorUtility.SetDirty(root);
}
Decorator decorator = parent as Decorator;
if (decorator)
{
Undo.RecordObject(decorator, "Behavior Tree (AddChild)");
decorator.child = child;
EditorUtility.SetDirty(decorator);
}
Composite composite = parent as Composite;
if (composite)
{
Undo.RecordObject(composite, "Behavior Tree (AddChild)");
composite.children.Add(child);
EditorUtility.SetDirty(composite);
}
}
public void RemoveChild(Node parent, Node child)
{
Root root = parent as Root;
if (root)
{
Undo.RecordObject(root, "Behavior Tree (RemoveChild)");
root.child = null;
EditorUtility.SetDirty(root);
}
Decorator decorator = parent as Decorator;
if (decorator)
{
Undo.RecordObject(decorator, "Behavior Tree (RemoveChild)");
decorator.child = null;
EditorUtility.SetDirty(decorator);
}
Composite composite = parent as Composite;
if (composite)
{
Undo.RecordObject(composite, "Behavior Tree (RemoveChild)");
composite.children.Remove(child);
EditorUtility.SetDirty(composite);
}
}
#endif
#endregion
}
- 유니티 에디터에서만 가능한 GUID 쪽 관련 메서드들은 #if UNITY_EDITOR 로 묶어두고 빌드