美文网首页
easyAR和AssetBundle和脚本那点事

easyAR和AssetBundle和脚本那点事

作者: Mr_kvkk | 来源:发表于2017-11-29 16:16 被阅读0次

           最近几天都在研究unity3d的AssetBundle资源打包方式,很好很强大,用来实现资源的热更新非常方便,但是   有个问题就是不能把脚本打包到模型上,这个问题就非常蛋疼了,光有模型,没有脚本,效果实在太差,那这么办呢,通过几天的研究,终于解决了这个问题.

           思路:把脚本打包成dll的程序集,通过Assembly的反射获取到对应脚本,然后AddComponent到模型上 

     第一步:生成dll程序集

     具体可以看这篇文章 感谢作者 这里我放上旋转放大缩小的脚本

    using UnityEngine;

    using System.Collections;

    public class SelfRotate1 : MonoBehaviour {

    private Touch oldTouch1;  //上次触摸点1(手指1)

    private Touch oldTouch2;  //上次触摸点2(手指2)

    void Start()

    {

    }

    void Update () {

    //没有触摸

    if ( Input.touchCount <= 0 ){

    return;

    }

    //单点触摸, 水平上下旋转

    if( 1 == Input.touchCount ){

    Touch touch = Input.GetTouch (0);

    Vector2 deltaPos = touch.deltaPosition;

    transform.Rotate(Vector3.down  * deltaPos.x , Space.World);

    transform.Rotate(Vector3.right * deltaPos.y , Space.World);

    }

    //多点触摸, 放大缩小

    Touch newTouch1 = Input.GetTouch (0);

    Touch newTouch2 = Input.GetTouch (1);

    //第2点刚开始接触屏幕, 只记录,不做处理

    if( newTouch2.phase == TouchPhase.Began ){

    oldTouch2 = newTouch2;

    oldTouch1 = newTouch1;

    return;

    }

    //计算老的两点距离和新的两点间距离,变大要放大模型,变小要缩放模型

    float oldDistance = Vector2.Distance(oldTouch1.position, oldTouch2.position);

    float newDistance = Vector2.Distance(newTouch1.position, newTouch2.position);

    //两个距离之差,为正表示放大手势, 为负表示缩小手势

    float offset = newDistance - oldDistance;

    //放大因子, 一个像素按 0.01倍来算(100可调整)

    float scaleFactor = offset / 100f;

    Vector3 localScale = transform.localScale;

    Vector3 scale = new Vector3(localScale.x + scaleFactor,

    localScale.y + scaleFactor,

    localScale.z + scaleFactor);

    //最小缩放到 0.3 倍

    if (scale.x > 0.3f && scale.y > 0.3f && scale.z > 0.3f) {

    transform.localScale = scale;

    }

    //记住最新的触摸点,下次使用

    oldTouch1 = newTouch1;

    oldTouch2 = newTouch2;

    }

    }

     第二步 把dll放到unity3d里面

          首先你需要把    xxxx.dll文件加上.bytes变成    xxx.dll.bytes,然后把这个文件放到项目Assets下面,在Assets下面新建文件夹Editor新建一个脚本 内容如下

        using System.Collections;

        using System.Collections.Generic; 

       using UnityEditor;

        using UnityEngine;

    public class ab  {

    [MenuItem("Assets/Build AssetBundle From Selection")]

    static void BuildAsset()

    {

    var se = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);

    foreach (var o in se)

    {

    string sp = AssetDatabase.GetAssetPath(o);

    string tar = Application.streamingAssetsPath + "/" + o.name + ".unity3d";

    if (!BuildPipeline.BuildAssetBundle(o, null, tar, BuildAssetBundleOptions.CollectDependencies, BuildTarget.Android))

    {

    Debug.Log(tar);

    }

    }

    AssetDatabase.Refresh();

    }

    }

    最后选中xxx.dll文件  右键选择AssetBundle From Selection导入资源到StreamingAssets 

     第三步 加载脚本到模型      

      加载代码如下通过反射获取对应的脚本 

       public static readonly string PathURL = 

    #if UNITY_ANDROID  "jar:file://" + Application.dataPath + "!/assets/" + "script.dll.unity3d";

     #elif UNITY_IPHONE  Application.dataPath + "/Raw/+"script.dll.unity3d";  

     #elif UNITY_STANDALONE_WIN || UNITY_EDITOR  "file://" + Application.dataPath + "/StreamingAssets/"+ "script.dll.unity3d";                 

     #else   string.Empty;  

     #endif               

     //获取dll脚本           

     public IEnumerator getScriptType(){ 

                   Debug.Log("path=" + PathURL);

                    WWW www = new WWW(PathURL); 

                   yield return www;

                    TextAsset tex = www.assetBundle.LoadAsset("script.dll");

                     Assembly ass = Assembly.Load(tex.bytes);

                    Type    type = ass.GetType("script.SelfRotate1");

    }

    最后只要把type加到模型下就大功告成了gameObject.AddComponent(type);

    相关文章

      网友评论

          本文标题: easyAR和AssetBundle和脚本那点事

          本文链接:https://www.haomeiwen.com/subject/rwfpbxtx.html