
[code]

      Type type = Type.GetType(typeof(Texture).FullName);
    
[/code]

Unityで上記のC#のスクリプトの帰り値がnullになります。


CC BY-SA 3.0
2014-12-12T13:42:27.337
1552
2014-12-12T14:41:44.477
2014-12-12T14:41:44.477
2129
2129
question
unity3d
Type.GetType(string);の帰り値がnullになる


以下のanswer.unity3dに回答がありました。

<http://answers.unity3d.com/questions/206665/typegettypestring-does-not-work-
in-unity.html>

以下のwrapperで対応する事が推奨されています。

[code]

    public class WrapClass
    {
            public static Type GetType (string typeName)
            {
    
                    // Try Type.GetType() first. This will work with types defined
                    // by the Mono runtime, in the same assembly as the caller, etc.
                    var type = Type.GetType (typeName);
    
                    // If it worked, then we're done here
                    if (type != null)
                            return type;
    
                    // If the TypeName is a full name, then we can try loading the defining assembly directly
                    if (typeName.Contains (".")) {
    
                            // Get the name of the assembly (Assumption is that we are using 
                            // fully-qualified type names)
                            var assemblyName = typeName.Substring (0, typeName.IndexOf ('.'));
    
                            // Attempt to load the indicated Assembly
                            var assembly = Assembly.Load (assemblyName);
                            if (assembly == null)
                                    return null;
    
                            // Ask that assembly to return the proper Type
                            type = assembly.GetType (typeName);
                            if (type != null)
                                    return type;
    
                    }
    
                    // If we still haven't found the proper type, we can enumerate all of the 
                    // loaded assemblies and see if any of them define the type
                    var currentAssembly = Assembly.GetExecutingAssembly ();
                    var referencedAssemblies = currentAssembly.GetReferencedAssemblies ();
                    foreach (var assemblyName in referencedAssemblies) {
    
                            // Load the referenced assembly
                            var assembly = Assembly.Load (assemblyName);
                            if (assembly != null) {
                                    // See if that assembly defines the named type
                                    type = assembly.GetType (typeName);
                                    if (type != null)
                                            return type;
                            }
                    }
    
                    // The type just couldn't be found...
                    return null;
            }
    }
    
[/code]

なお Unity5(beta)でも問題は解決していませんでした。


CC BY-SA 3.0
2014-12-12T13:45:02.717
1553
2014-12-12T13:45:02.717
2129
1552
answer