登录 用户中心() [退出] 后台管理 注册
   
您的位置: 首页 >> SoftHub关联区 >> 主题: [u3d]unity3d中给GameObject绑定脚本的代码[zt]     [回主站]     [分站链接]
[u3d]unity3d中给GameObject绑定脚本的代码[zt]
clq
浏览(265) - 2022-11-02 21:37:48 发表 编辑

关键字:

[2022-11-02 22:38:26 最后更新]
[u3d]unity3d中给GameObject绑定脚本的代码[zt]

https://www.cnblogs.com/eniac1946/p/7233640.html


unity3d中给GameObject绑定脚本的代码
Posted on 2017-07-25 13:36  AYE89  阅读(9365)  评论(0)  编辑  收藏  举报

一、获取GameObject

1.GameObject.Find()

通过场景里面的名子或者一个路径直接获取游戏对象。
    GameObject root = GameObject.Find(“GameObject”);

我觉得如果游戏对象没再最上层,那么最好使用路径的方法,因为有可能你的游戏对象会有重名的情况,路径用“/”符号隔开即可。
    GameObject root = GameObject.Find(“GameObject/Cube”);

 

二、添加或删除一个脚本
复制代码

//tempQie2为GameObject,qiemove为自定义的脚本类名称  
 
tempQie2.AddComponent<qiemove>();//添加绑定脚本  
 
Destroy(tempQie2.GetComponent("qiemove"));//删除绑定脚本  
 
//如果添加其他属性,可AddComponent<其他类定义>();  

复制代码

 

三、动态加载一个(外部)脚本
复制代码

var fs = new FileStream(@"D:\Personal\My Documents\Projects\TestLib\TestLib\bin\Release\TestLib.dll", FileMode.Open);  
 var b = new byte[fs.Length];  
 fs.Read(b, 0, b.Length);  
 fs.Close();  
 var assembly = System.Reflection.Assembly.Load(b);  
 var type = assembly.GetType("Test");  
 gameObject.AddComponent(type);

复制代码

分析这段代码

 

加载里一个DLL,这个DLL实际上是用C#打包的代码库,关于对库的各种叫法实在让人蛋疼,不提也罢。总之建立一个工程然后引用U3D的库UnityEngine.dll就可以编译,不引用UnityEngine.dll当然首先就没法通过编译。

我这里的类名就叫Test,所以获取类型就是这样的。这里添加组件就不能用AddComponent(string)方法,那样会提示找不到了,可能这个方法只是从字典里面找到相应的类型然后在用AddComponent(type)来添加。

 

要吐槽的是关于那个二进制流。看网上很多WWW来读取TextAsset然后转成byte[]。事实上我不管怎么试都是失败。与干脆用C#自带的函数,就实际情况来说虽然统一使用WWW会比较方便,不过即使用C#来做网络下载难度也不会太大。

补充:WINDOWS下可以用        var assembly = System.Reflection.Assembly.LoadFile(@"D:\TestLib1.dll");

Andriod下只能用      var assembly = System.Reflection.Assembly.Load(b);

 

  2) (未试过)

本文记录如何通过unity3d进行脚本资源打包加载

a、创建TestDll.cs文件

public class TestDll : MonoBehaviour {
    void Start () {
        print("Hi U_tansuo!");
    }
}

b、生成dll文件

   (1)使用vs打包

  (2) 使用mono打包

    (3) 命令行打包 mac下(亲测):  /Applications/Unity/Unity.app/Contents/Frameworks/Mono/bin/gmcs -r:/Applications/Unity/Unity.app/Contents/Frameworks/Managed/UnityEngine.dll -target:library 脚本路径

                        win下(未试过):mcs -r: /unity安装根目录\Unity\Editor\Data\Managed/UnityEngine.dll -target:library 脚本路径

c、更改文件后缀

      至关重要一步  更改上一步生成的TestDLL.dll 为 TestDLL.bytes  否则 打包加载会错

d、使用 BuildPipeline.BuildAssetBundle进行打包 资源为 TestDll.unity3d

e、加载
复制代码

    IEnumerator Test()
    {
    
            string url="file://"+Application.dataPath+"/TestDll.unity3d";
        print(url);
          WWW www = WWW.LoadFromCacheOrDownload (url, 1);

    // Wait for download to complete
    yield return www;
        
    // Load and retrieve the AssetBundle
    AssetBundle bundle = www.assetBundle;
//TestDll 是资源的名字
        TextAsset txt = bundle.Load("TestDll", typeof(TextAsset)) as TextAsset;
        print(txt.bytes.Length);
    // Load the assembly and get a type (class) from it
    var assembly = System.Reflection.Assembly.Load(txt.bytes);
    var type = assembly.GetType("TestDll");

    // Instantiate a GameObject and add a component with the loaded class
    
    gameObject.AddComponent(type);
    }

复制代码

 
标签: script, unity3d



clq  2022-11-02 22:38:26 发表 编辑

这个 mcs.exe 在 windows 上基本是在 F:\new\u3d2022\2021.3.2f1c1\Editor\Data\MonoBleedingEdge\lib\mono\4.5 这样的目录中.

总数:1 页次:1/1 首页 尾页  
总数:1 页次:1/1 首页 尾页  


所在合集/目录



发表评论:
文本/html模式切换 插入图片 文本/html模式切换


附件:



NEWBT官方QQ群1: 276678893
可求档连环画,漫画;询问文本处理大师等软件使用技巧;求档softhub软件下载及使用技巧.
但不可"开车",严禁国家敏感话题,不可求档涉及版权的文档软件.
验证问题说明申请入群原因即可.

Copyright © 2005-2020 clq, All Rights Reserved
版权所有
桂ICP备15002303号-1