Skip to content

Instantly share code, notes, and snippets.

@anchan828
Last active August 29, 2015 14:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anchan828/3c38e5031d2881b8340b to your computer and use it in GitHub Desktop.
Save anchan828/3c38e5031d2881b8340b to your computer and use it in GitHub Desktop.
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]);
}
}
[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