Unity的cs脚本文件属于MonoScript资源,先来看看MonoScript的类定义
using System;
using UnityEngine;
using UnityEngine.Bindings;
namespace UnityEditor
{
//
// 摘要:
// Representation of Script assets.
[NativeClass(null)]
[NativeType("Editor/Mono/MonoScript.bindings.h")]
public class MonoScript : TextAsset
{
public MonoScript();
//
// 摘要:
// Returns the MonoScript object containing specified MonoBehaviour.
//
// 参数:
// behaviour:
// The MonoBehaviour whose MonoScript should be returned.
[FreeFunction]
public static MonoScript FromMonoBehaviour(MonoBehaviour behaviour);
//
// 摘要:
// Returns the MonoScript object containing specified ScriptableObject.
//
// 参数:
// scriptableObject:
// The ScriptableObject whose MonoScript should be returned.
[FreeFunction]
public static MonoScript FromScriptableObject(ScriptableObject scriptableObject);
//
// 摘要:
// Returns the System.Type object of the class implemented by this script.
public Type GetClass();
}
}
MonoScript.FromMonoBehaviour
来通过MonoBehaviour
得到脚本MonoScript
,MonoScript.text
来得到整个类脚本文本
MonoScript.GetClass()
这个方法有点特殊,返回Type
- Case 1 : 如果脚本fileName和它定义的类没有一个相同时,返回null。如下面脚本文件名为SomeClass.cs
namespace TestFrame
{
public class ClassA
{
}
public class ClassB
{
}
}
- Case 2 : 如果把上面的ClassB改成和文件名一致,则会返回SomeClass
namespace TestFrame
{
public class ClassA
{
}
public class SomeClass
{
}
}
- Case 3 :如果类名和文件名一直,但是类是泛型,返回结果也为null。如下面定义在文件SomeGenericClass.cs文件中
namespace TestFrame
{
public class SomeGenericClass<T>
{
}
}
网友评论