Skip to content

Instantly share code, notes, and snippets.

@fum1h1ro
Created April 22, 2012 07:40
Show Gist options
  • Select an option

  • Save fum1h1ro/2462537 to your computer and use it in GitHub Desktop.

Select an option

Save fum1h1ro/2462537 to your computer and use it in GitHub Desktop.
Unity3Dでデバッグメニュー
using UnityEngine;
using System.Collections;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
// 実装例
public class DebugMenu : DebugMenuBase {
public bool playerMoveSync { get { return hoge_.playerMoveSync_; } }
[System.Serializable]
public class Hoge {
public bool playerMoveSync_;
public int id_;
public string temp_;
public Hoge() {
playerMoveSync_ = false;
id_ = 0;
temp_ = "HAGE";
}
}
private Hoge hoge_;
void Awake() {
base.Awake();
Rect rc = this.menuWindowRect;
rc.y += CalcVerticalInRetina(20.0f); // status bar
this.menuWindowRect = rc;
SetDelegate(OnDelegate);
hoge_ = (Hoge)Load();
if (hoge_ == null) hoge_ = new Hoge();
}
void OnDelegate(int id) {
hoge_.playerMoveSync_ = GUILayout.Toggle(hoge_.playerMoveSync_, "PlayerMoveSync");
}
void OnDestroy() {
Save(hoge_);
}
}
// デバッグメニュー基底
public class DebugMenuBase : MonoBehaviour {
private Rect rect_;
private float touched_;
private Rect menurect_;
public GUIStyle boxstyle_;
private GUI.WindowFunction delegateFunction_;
private string windowTitle_;
public Texture2D texture_;
const float kRETINA_W = 640.0f;
const float kRETINA_H = 960.0f;
const float kBUTTON_SIZE = 96.0f;
enum State {
kBUTTON,
kWAKEUP,
kSHUTDOWN,
kDELEGATE,
};
private State state_;
private bool first_;
private float interp_;
private float scaleRetinaW_, scaleRetinaH_;
//
private float buttonWidth { get { return rect_.width; } }
private float buttonHeight { get { return rect_.height; } }
private float halfButtonWidth { get { return rect_.width * 0.5f; } }
private float halfButtonHeight { get { return rect_.height * 0.5f; } }
protected Rect menuWindowRect { get { return menurect_; } set { menurect_ = value; } }
public void SetDelegate(GUI.WindowFunction func) { delegateFunction_ = func; }
public string windowTitle { get { return windowTitle_; } set { windowTitle_ = value; } }
private State state { get { return state_; } set { first_ = true; state_ = value; } }
protected void Awake() {
scaleRetinaW_ = Screen.width / kRETINA_W;
scaleRetinaH_ = Screen.height / kRETINA_H;
float w = CalcHorizontalInRetina(kBUTTON_SIZE);
float h = CalcVerticalInRetina(kBUTTON_SIZE);
this.state = State.kBUTTON;
rect_ = new Rect(0, 0, w, h);
touched_ = 0.0f;
menurect_ = new Rect(0, 0, Screen.width, Screen.height * 0.7f);
delegateFunction_ = DefaultDelegate;
this.windowTitle = "DEBUG MENU";
}
protected virtual void OnPush() {
}
protected virtual void DefaultDelegate(int id) {
}
void OnGUI() {
bool f = first_;
switch (this.state) {
case State.kBUTTON:
StateButton();
break;
case State.kWAKEUP:
StateWakeup();
break;
case State.kSHUTDOWN:
StateShutdown();
break;
case State.kDELEGATE:
StateDelegate();
break;
}
if (f && first_) first_ = false;
}
void StateButton() {
Event e = Event.current;
//if (e.type != EventType.Used)
if (touched_ == 0.0f) {
if (e.type == EventType.MouseDown && rect_.Contains(e.mousePosition)) {
touched_ += Time.deltaTime;
e.Use();
} else {
AdjustRect();
}
} else {
touched_ += Time.deltaTime;
if (touched_ < 0.5f) {
if (e.type == EventType.MouseUp) {
if (rect_.Contains(e.mousePosition)) {
this.state = State.kWAKEUP;
}
touched_ = 0.0f;
}
} else {
// move
rect_.x = e.mousePosition.x - rect_.width * 0.5f;
rect_.y = e.mousePosition.y - rect_.height * 0.5f;
if (e.type == EventType.MouseUp) {
touched_ = 0.0f;
}
}
if (e.isMouse) e.Use();
}
LimitRect();
RenderButton();
}
Rect LerpRect(Rect from, Rect to, float t) {
Rect rc = new Rect(0, 0, 0, 0);
rc.x = Mathf.Lerp(from.x, to.x, t);
rc.y = Mathf.Lerp(from.y, to.y, t);
rc.width = Mathf.Lerp(from.width, to.width, t);
rc.height = Mathf.Lerp(from.height, to.height, t);
return rc;
}
protected float CalcHorizontalInRetina(float v) {
return scaleRetinaW_ * v;
}
protected float CalcVerticalInRetina(float v) {
return scaleRetinaH_ * v;
}
void StateWakeup() {
if (first_) {
interp_ = 0.0f;
}
Rect rc = LerpRect(rect_, menurect_, interp_);
interp_ += (1.0f / 0.5f) * Time.deltaTime;
if (interp_ >= 1.0f) {
this.state = State.kDELEGATE;
}
rc = GUI.Window(0, rc, delegateFunction_/*DefaultDelegate*/, windowTitle_);
//rc = GUI.Window(0, rc, DefaultDelegate, windowTitle_);
}
void StateShutdown() {
if (first_) {
interp_ = 0.0f;
}
Rect rc = LerpRect(menurect_, rect_, interp_);
interp_ += (1.0f / 0.5f) * Time.deltaTime;
if (interp_ >= 1.0f) {
this.state = State.kBUTTON;
}
rc = GUI.Window(0, rc, delegateFunction_/*DefaultDelegate*/, windowTitle_);
//rc = GUI.Window(0, rc, DefaultDelegate, windowTitle_);
}
void StateDelegate() {
Event e = Event.current;
if (e.type == EventType.MouseDown && !menurect_.Contains(e.mousePosition)) {
this.state = State.kSHUTDOWN;
e.Use();
}
menurect_ = GUI.Window(0, menurect_, delegateFunction_, windowTitle_);//, boxstyle_);
}
void LimitRect() {
float w = this.buttonWidth;
float h = this.buttonHeight;
rect_.x = Mathf.Clamp(rect_.x, 0.0f, Screen.width - w);
rect_.y = Mathf.Clamp(rect_.y, 0.0f, Screen.height - h);
}
void AdjustRect() {
float x = rect_.x + this.halfButtonWidth;
float y = rect_.y;
float nx = (x < Screen.width * 0.5f)? 0.0f : Screen.width;
float ny = y;
rect_.x += (nx - x) * 0.07f;
rect_.y += (ny - y) * 0.07f;
}
void RenderButton() {
GUI.Box(rect_, "DEBUG");//, boxstyle_);
}
private Stream CreatePersistentDataStream(FileMode mode) {
Stream stream = new FileStream(Application.persistentDataPath + "/debugmenu.bin", mode);
return stream;
}
protected void Save(System.Object obj) {
using (Stream stream = CreatePersistentDataStream(FileMode.Create)) {
BinaryFormatter fmt = new BinaryFormatter();
fmt.Serialize(stream, obj);
stream.Close();
}
}
protected System.Object Load() {
System.Object obj;
try {
using (Stream stream = CreatePersistentDataStream(FileMode.Open)) {
BinaryFormatter fmt = new BinaryFormatter();
obj = fmt.Deserialize(stream);
}
}
catch (FileNotFoundException e) {
obj = null;
}
return obj;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment