Skip to content

Commit

Permalink
スクリプトファイルから直接メソッドが実行できるようになりました。 コルーチンにも対応しました。
Browse files Browse the repository at this point in the history
  • Loading branch information
rngtm committed Nov 11, 2016
1 parent dab8bf1 commit 5944c8c
Show file tree
Hide file tree
Showing 15 changed files with 801 additions and 292 deletions.
Binary file modified Assets/EasyScriptTester/Demos/Scenes/Demo.unity
Binary file not shown.
13 changes: 12 additions & 1 deletion Assets/EasyScriptTester/Demos/Scripts/Cube.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,20 @@
namespace EasyScriptTester.Demos
{
using UnityEngine;
using System.Collections;

/// <summary>
/// Demo用のクラス
/// </summary>
public class Cube : MonoBehaviour
{
[SerializeField]
private bool isRotate = false;

/// <summary>
/// 回転開始
/// </summary>
void StartRotation()
void StartRotation(int x)
{
this.isRotate = true;
}
Expand All @@ -36,5 +38,14 @@ void Update()
this.transform.Rotate(new Vector3(2f, 3f, 4f));
}
}

IEnumerator TestCoroutine(int x, int y, int z)
{
Debug.Log(x);
yield return new WaitForSeconds(1f);
Debug.Log(y);
yield return new WaitForSeconds(1f);
Debug.Log(z);
}
}
}
131 changes: 131 additions & 0 deletions Assets/EasyScriptTester/Editor/ComponentData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
///-----------------------------------
/// EasyScriptTester
/// @ 2016 RNGTM(https://github.com/rngtm)
///-----------------------------------
namespace EasyScriptTester
{
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Linq;

/// <summary>
/// Component data
/// </summary>
public class ComponentData
{
#region Fields
/// <summary>
/// コンポーネント取得に成功した
/// </summary>
public bool IsSuccess { get; private set; }

/// <summary>
/// コンポーネントのType
/// </summary>
public Type ComponentType { get; private set; }

/// <summary>
/// オブジェクトの種類
/// </summary>
public ObjectType ObjectType { get; private set; }

/// <summary>
/// このコンポーネントを持っているオブジェクト
/// </summary>
public Object Object { get; private set; }

/// <summary>
/// このコンポーネントが持つメソッドの情報
/// </summary>
public MethodData[] MethodDatas { get; private set; }
#endregion Fields

/// <summary>
/// コンストラクタ
/// </summary>
public ComponentData(Object obj, ObjectType objType, Type componentType)
{
if (componentType == null)
{
this.IsSuccess = false;
return;
}

this.IsSuccess = true;
this.Object = obj;
this.ObjectType = objType;
this.ComponentType = componentType;
this.MethodDatas = ExtractMethods(componentType)
.Select(methodInfo => new MethodData(methodInfo))
.ToArray();
}

/// <summary>
/// メソッドの一括取得
/// </summary>
public static IEnumerable<MethodInfo> ExtractMethods(System.Type type)
{
var methods = type.GetMethods(
BindingFlags.Static
| BindingFlags.Public
| BindingFlags.NonPublic
| BindingFlags.Instance
);

return methods
.Where(m => m.DeclaringType == type)
.Where(m => m.Name[0] != '<')
.Where(m => IsProperty(m) == false);
}

/// <summary>
/// Propertyかどうか
/// </summary>
private static bool IsProperty(MethodInfo methodInfo)
{
switch (methodInfo.Name.Split('_')[0])
{
case "get":
case "set":
return true;
default:
return false;
}
}

/// <summary>
/// 変数・プロパティの一括取得
/// </summary>
public static IEnumerable<MemberData> ExtractMembers(object obj)
{
var type = obj.GetType();
var members = type.GetMembers(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance);

foreach (var member in members)
{
if (member.MemberType == MemberTypes.Field)
{
var field = (FieldInfo)member;
yield return new MemberData
{
Name = field.Name,
Value = field.GetValue(obj),
};
continue;
}

if (member.MemberType == MemberTypes.Property)
{
var property = (PropertyInfo)member;
yield return new MemberData
{
Name = property.Name,
Value = property.GetValue(obj, null),
};
continue;
}
}
}
}
}
12 changes: 12 additions & 0 deletions Assets/EasyScriptTester/Editor/ComponentData.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 5944c8c

Please sign in to comment.