Last active
August 29, 2015 14:19
-
-
Save anchan828/3c38e5031d2881b8340b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using System.Linq; | |
using System.Reflection; | |
using UnityEditor; | |
using UnityEngine; | |
class HierarchyWindowHelper | |
{ | |
private SerializedObject so; | |
private SerializedProperty expandedIDsProperty | |
{ | |
get | |
{ | |
return so.FindProperty("m_TreeViewState.m_ExpandedIDs"); | |
} | |
} | |
public HierarchyWindowHelper(EditorWindow hierarchyWindow) | |
{ | |
so = new SerializedObject(hierarchyWindow); | |
} | |
public List<int> GetExpandedIDs() | |
{ | |
so.Update(); | |
var expandedIDs = new List<int>(); | |
for (int i = 0; i < expandedIDsProperty.arraySize; i++) | |
{ | |
expandedIDs.Add(expandedIDsProperty.GetArrayElementAtIndex(i).intValue); | |
} | |
return expandedIDs; | |
} | |
public List<string> GetExpandedNames() | |
{ | |
return GetExpandedIDs() | |
.Select(expandedID => EditorUtility.InstanceIDToObject(expandedID)) | |
.OfType<GameObject>() | |
.Select(gameObject => GetAbsoluteGameObjectName(gameObject.transform)) | |
.ToList(); | |
} | |
private string GetAbsoluteGameObjectName(Transform transform) | |
{ | |
if (transform.parent) | |
{ | |
return GetAbsoluteGameObjectName(transform.parent) + "/" + transform.name; | |
} | |
return transform.name; | |
} | |
public void SetExpandedNames(List<string> expandedNames) | |
{ | |
var expandedIDs = expandedNames | |
.Select(expandedName => GameObject.Find(expandedName)) | |
.Where(gameObject => gameObject != null) | |
.Select(gameObject => gameObject.GetInstanceID()).ToList(); | |
SetExpandedIDs(expandedIDs); | |
} | |
public void SetExpandedIDs(List<int> expandedIDs) | |
{ | |
so.Update(); | |
expandedIDsProperty.ClearArray(); | |
expandedIDsProperty.arraySize = expandedIDs.Count; | |
for (var i = 0; i < expandedIDsProperty.arraySize; i++) | |
{ | |
expandedIDsProperty.GetArrayElementAtIndex(i).intValue = expandedIDs[i]; | |
} | |
so.ApplyModifiedProperties(); | |
Reload(); | |
} | |
public void Reload() | |
{ | |
var methodInfo = so.targetObject.GetType().GetMethod("ReloadData", BindingFlags.Instance | BindingFlags.Public); | |
methodInfo.Invoke(so.targetObject, new object[0]); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[MenuItem("TestMenu/Build")] | |
static void Buid() | |
{ | |
var type = Types.GetType("UnityEditor.SceneHierarchyWindow", "UnityEditor.dll"); | |
// 特定のHierarchyウィンドウを取得する | |
var hierarchyWindow = EditorWindow.GetWindow(type); | |
HierarchyWindowHelper hierarchyWindowHelper = new HierarchyWindowHelper(hierarchyWindow); | |
// ExpandedNames を取得する | |
var expandedNames = hierarchyWindowHelper.GetExpandedNames(); | |
BuildPipeline.BuildPlayer(new string[0], "Test", BuildTarget.WebPlayer, BuildOptions.None); | |
// ExpandedNames を設定する | |
hierarchyWindowHelper.SetExpandedNames(expandedNames); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment