Skip to content

Instantly share code, notes, and snippets.

@CM3D2-01
Created October 10, 2015 16:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save CM3D2-01/adcf5072ff5ba812858a to your computer and use it in GitHub Desktop.
Save CM3D2-01/adcf5072ff5ba812858a to your computer and use it in GitHub Desktop.
CM3D2:SystemShortcutにボタン追加
using System;
using System.Reflection;
using UnityEngine;
using UnityInjector.Attributes;
namespace CM3D2.SystemShortcutExtension.Plugin
{
[PluginFilter("CM3D2x64"), PluginFilter("CM3D2x86"), PluginFilter("CM3D2VRx64")]
[PluginName("CM3D2 SystemShortcutExtension"), PluginVersion("0.0.0.0")]
public class SystemShortcutExtension : UnityInjector.PluginBase
{
public void OnLevelWasLoaded(int level) { if (level == 9) testAddButton(); }
public void OnClick_Test1() { Debug.LogWarning("Test1 is clicked."); }
public void OnClick_Test2() { Debug.LogWarning("Test2 is clicked."); }
public void OnClick_Test3() { Debug.LogWarning("Test3 is clicked."); }
private void testAddButton()
{
// システムショートカットオブジェクトへの参照取得
GameObject goSystemShortcut = GameObject.Find("__GameMain__/SystemUI Root/SystemShortcut");
GameObject goShortcutBase = GameObject.Find("__GameMain__/SystemUI Root/SystemShortcut/Base");
GameObject goShortcutGrid = GameObject.Find("__GameMain__/SystemUI Root/SystemShortcut/Base/Grid");
GameObject goConfigButton = GameObject.Find("__GameMain__/SystemUI Root/SystemShortcut/Base/Grid/Config");
SystemShortcut systemShortcut = goSystemShortcut.GetComponent<SystemShortcut>();
UIGrid uiShortcutGrid = goShortcutGrid.GetComponent<UIGrid>();
// コンフィグパネル呼び出しボタンを複製してコールバック削除
EventDelegate orgOnClick_Config = GetFieldValue<SystemShortcut, EventDelegate[]>(systemShortcut, "m_aryDgOnClick")[0];
GameObject goConfigButtonCopy = UnityEngine.Object.Instantiate(goConfigButton) as GameObject;
EventDelegate.Remove(goConfigButtonCopy.GetComponent<UIButton>().onClick, orgOnClick_Config);
// 複製したボタンをさらに複製して、
// コールバックとポップアップテキストを追加しショートカットメニューに追加
GameObject goButtonTest1 = UnityEngine.Object.Instantiate(goConfigButtonCopy) as GameObject;
EventDelegate.Add(goButtonTest1.GetComponent<UIButton>().onClick, this.OnClick_Test1);
UIEventTrigger uiEventTrigger1 = goButtonTest1.GetComponent<UIEventTrigger>();
EventDelegate.Add( uiEventTrigger1.onHoverOver, delegate { systemShortcut.VisibleExplanation("Test1", true); } );
EventDelegate.Add( uiEventTrigger1.onHoverOut, delegate { systemShortcut.VisibleExplanation("Test1", false); } );
EventDelegate.Add( uiEventTrigger1.onDragStart, delegate { systemShortcut.VisibleExplanation("Test1", false); } );
SetChild(goShortcutGrid, goButtonTest1);
GameObject goButtonTest2 = UnityEngine.Object.Instantiate(goConfigButtonCopy) as GameObject;
EventDelegate.Add(goButtonTest2.GetComponent<UIButton>().onClick, this.OnClick_Test2);
UIEventTrigger uiEventTrigger2 = goButtonTest2.GetComponent<UIEventTrigger>();
EventDelegate.Add( uiEventTrigger2.onHoverOver, delegate { systemShortcut.VisibleExplanation("Test2", true); } );
EventDelegate.Add( uiEventTrigger2.onHoverOut, delegate { systemShortcut.VisibleExplanation("Test2", false); } );
EventDelegate.Add( uiEventTrigger2.onDragStart, delegate { systemShortcut.VisibleExplanation("Test2", false); } );
SetChild(goShortcutGrid, goButtonTest2);
GameObject goButtonTest3 = UnityEngine.Object.Instantiate(goConfigButtonCopy) as GameObject;
EventDelegate.Add(goButtonTest3.GetComponent<UIButton>().onClick, this.OnClick_Test3);
UIEventTrigger uiEventTrigger3 = goButtonTest3.GetComponent<UIEventTrigger>();
EventDelegate.Add( uiEventTrigger3.onHoverOver, delegate { systemShortcut.VisibleExplanation("Test3", true); } );
EventDelegate.Add( uiEventTrigger3.onHoverOut, delegate { systemShortcut.VisibleExplanation("Test3", false); } );
EventDelegate.Add( uiEventTrigger3.onDragStart, delegate { systemShortcut.VisibleExplanation("Test3", false); } );
SetChild(goShortcutGrid, goButtonTest3);
// ショートカットの白い枠のサイズ調整
//goShortcutBase.transform.localScale = Vector3.Scale(goShortcutBase.transform.localScale, new Vector3(3f, 1f, 1f));
//goShortcutBase.transform.localPosition = goShortcutBase.transform.localPosition - new Vector3(0f, 0f, 0f);
/*foreach (Transform child in goShortcutGrid.transform)
{
// 親を横に引き伸ばした分、子を縮小しておく
child.transform.localScale = Vector3.Scale(goShortcutBase.transform.localScale, new Vector3(0.33f, 1f, 1f));
}*/
//(ボタンも伸びて上手くいかない・・・ UISpriteの画像を変更する必要があるかも)
uiShortcutGrid.Reposition();
goConfigButtonCopy.SetActive(false); //ストックしたボタンは非表示にしておく
}
internal static void SetChild(GameObject parent, GameObject child)
{
child.layer = parent.layer;
child.transform.parent = parent.transform;
child.transform.localPosition = Vector3.zero;
child.transform.localScale = Vector3.one;
child.transform.rotation = Quaternion.identity;
}
internal static FieldInfo GetFieldInfo<T>(string name)
{
BindingFlags bf = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance;
return typeof(T).GetField(name, bf);
}
internal static TResult GetFieldValue<T, TResult>(T inst, string name)
{
if (inst == null) return default(TResult);
FieldInfo field = GetFieldInfo<T>(name);
if (field == null) return default(TResult);
return (TResult)field.GetValue(inst);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment