//LineRenderer line;
    public void LoadAttack(InventoryItem weapon, MeteorUnit target, AttackDes attackdef, MeteorUnit Owner)
    {
        //line = gameObject.AddComponent<LineRenderer>();
        //line.startWidth = 1f;
        //line.endWidth = 1f;
        //line.numPositions = 200;
        owner       = Owner;
        _attack     = attackdef;
        auto_target = target;
        WeaponRoot  = new GameObject().transform;
        WeaponRoot.SetParent(transform);
        WeaponRoot.localPosition    = Vector3.zero;
        WeaponRoot.localScale       = Vector3.one;
        WeaponRoot.localRotation    = Quaternion.Euler(0, 0, 0);
        WeaponRoot.name             = "WeaponRoot";
        WeaponRoot.gameObject.layer = gameObject.layer;
        Weapon = weapon;
        //计算控制点
        spline.SetControlPoint(0, transform.position);
        InitSpline();
        //List<Vector3> veclst = spline.GetEquiDistantPointsOnCurve(200);
        //line.SetPositions(veclst.ToArray());
        tTotal = spline.GetLength() / speed;
        //Debug.LogError("tTotal Init:" + tTotal + " length:" + spline.GetLength() + " speed:" + speed);
        LoadWeapon();
        //增加拖尾
        GameObject trailS = NodeHelper.Find("d_wpnRS", this.gameObject);
        GameObject trailE = NodeHelper.Find("d_wpnRE", this.gameObject);

        Trail = gameObject.AddComponent <WeaponTrail>();
        if (trailS != null)
        {
            Trail.AddTransform(trailS.transform);
        }
        if (trailE != null)
        {
            Trail.AddTransform(trailE.transform);
        }
        Trail.Init(Owner);
        Trail.Open();
        MeshRenderer mr = gameObject.GetComponentInChildren <MeshRenderer>();

        if (mr != null)
        {
            BoxCollider bc = mr.gameObject.AddComponent <BoxCollider>();
            bc.isTrigger = true;
            bc.size      = new Vector3(10, 5, 10);
        }
    }
    //把背包里的物品,装备到身上.
    public void EquipWeapon(InventoryItem item, bool ModelLayer = false)
    {
        if (Weapon == null && item != null)
        {
            if (item.Info().MainType == (int)EquipType.Weapon)
            {
                float scale = 1.0f;
                if (item.Info().Size != 0)//size为0的时候
                {
                    scale = (item.extra == null ? item.Info().Size : item.Info().Size *(1 + item.extra.SizePercent / 100.0f));
                }
                else
                {
                    scale = (item.extra == null ? 1 : (1 + item.extra.SizePercent / 100.0f));
                }
                WeaponDatas.WeaponDatas weaponProperty = U3D.GetWeaponProperty(item.Info().UnitId);


                string weaponL = "";
                string weaponR = "";
                PoseType = item.WeaponPos;
                if (item.WeaponPos == 0)
                {
                    weaponL = weaponProperty.WeaponL;
                    weaponR = weaponProperty.WeaponR;
                }
                else if (item.WeaponPos == 1)
                {
                    weaponL = weaponProperty.PosAWL;
                    weaponR = weaponProperty.PosAWR;
                }
                else if (item.WeaponPos == 2)
                {
                    weaponL = weaponProperty.PosBWL;
                    weaponR = weaponProperty.PosBWR;
                }

                //Debug.Log("加载武器:" + " 左手:" + weaponL + "右手:" + weaponR);
                if (!string.IsNullOrEmpty(weaponL))
                {
                    GameObject weaponPrefab = Resources.Load <GameObject>(weaponL);
                    if (weaponPrefab == null)
                    {
                        GMCFile fGmcL = Main.Ins.GMCLoader.Load(weaponL);
                        DesFile fDesL = Main.Ins.DesLoader.Load(weaponL);

                        if (fGmcL != null && fDesL != null)
                        {
                            GenerateWeaponModel(weaponL, fGmcL, fDesL, false, scale, weaponProperty.TextureL, ModelLayer);
                        }
                        else if (fGmcL == null && fDesL != null)
                        {
                            GMBFile fGmbL = Main.Ins.GMBLoader.Load(weaponL);
                            GenerateWeaponModel(weaponL, fGmbL, fDesL, false, scale, weaponProperty.TextureL, ModelLayer);
                        }
                    }
                    else
                    {
                        if (L != null)
                        {
                            DestroyImmediate(L);
                        }
                        GameObject objWeapon = GameObject.Instantiate(weaponPrefab);
                        objWeapon.layer = ModelLayer ? LayerMask.NameToLayer("RenderModel") : LayerMask.NameToLayer("Weapon");
                        L = objWeapon.transform;
                        //L = new GameObject().transform;
                        L.SetParent(LP);
                        L.localPosition = Vector3.zero;
                        //这种导入来的模型,需要Y轴旋转180,与原系统的物件坐标系存在一些问题
                        L.localRotation = new Quaternion(0, 1, 0, 0);
                        L.name          = weaponL;
                        L.localScale    = Vector3.one;
                        //WE = L;
                        //武器挂点必须在缩放正确后才能到指定的位置
                        WeaponTrail wt = L.gameObject.AddComponent <WeaponTrail>();
                        //右手
                        //GameObject ctrlRs = Global.ldaControlX("d_wpnRS", L.gameObject);
                        //if (ctrlRs != null)
                        //    wt.AddTransform(ctrlRs.transform);
                        //GameObject ctrlRe = Global.ldaControlX("d_wpnRE", L.gameObject);
                        //if (ctrlRe != null)
                        //    wt.AddTransform(ctrlRe.transform);
                        //左手
                        GameObject ctrlLs = NodeHelper.Find("d_wpnLS", L.gameObject);
                        if (ctrlLs != null)
                        {
                            wt.AddTransform(ctrlLs.transform);
                        }
                        GameObject ctrlLe = NodeHelper.Find("d_wpnLE", L.gameObject);
                        if (ctrlLe != null)
                        {
                            wt.AddTransform(ctrlLe.transform);
                        }

                        //每个武器只能有一个碰撞盒
                        BoxCollider box = L.GetComponentInChildren <BoxCollider>();
                        if (box != null)
                        {
                            box.enabled          = false;
                            box.gameObject.layer = ModelLayer ? LayerMask.NameToLayer("RenderModel") : LayerMask.NameToLayer("Weapon");
                            weaponDamage.Add(box);
                        }
                        else
                        {
                            Debug.LogError("新增武器上找不到碰撞盒[除了暗器火枪飞轮外都应该有碰撞盒]");
                        }
                        if (owner != null)
                        {
                            wt.Init(owner);
                            trail.Add(wt);
                        }
                        else
                        {
                            GameObject.Destroy(wt);
                            wt = null;
                        }
                    }
                }
                if (!string.IsNullOrEmpty(weaponR))
                {
                    GameObject weaponPrefab = Resources.Load <GameObject>(weaponR);
                    if (weaponPrefab == null)
                    {
                        GMCFile fGmcR = Main.Ins.GMCLoader.Load(weaponR);
                        DesFile fDesR = Main.Ins.DesLoader.Load(weaponR);
                        if (fGmcR != null && fDesR != null)
                        {
                            GenerateWeaponModel(weaponR, fGmcR, fDesR, true, scale, weaponProperty.TextureR, ModelLayer);
                        }
                        else if (fGmcR == null && fDesR != null)
                        {
                            GMBFile fGmbR = Main.Ins.GMBLoader.Load(weaponProperty.WeaponR);
                            GenerateWeaponModel(weaponR, fGmbR, fDesR, true, scale, weaponProperty.TextureR, ModelLayer);
                        }
                    }
                    else
                    {
                        if (R != null)
                        {
                            DestroyImmediate(R);
                        }
                        GameObject objWeapon = GameObject.Instantiate(weaponPrefab);
                        objWeapon.layer = ModelLayer ? LayerMask.NameToLayer("RenderModel") : LayerMask.NameToLayer("Weapon");
                        R = objWeapon.transform;
                        R.SetParent(RP);
                        R.localPosition = Vector3.zero;
                        R.localRotation = new Quaternion(0, 1, 0, 0);
                        R.name          = weaponR;
                        R.localScale    = Vector3.one;

                        //武器挂点必须在缩放正确后才能到指定的位置
                        WeaponTrail wt = R.gameObject.AddComponent <WeaponTrail>();
                        //右手
                        GameObject ctrlRs = NodeHelper.Find("d_wpnRS", R.gameObject);
                        if (ctrlRs != null)
                        {
                            wt.AddTransform(ctrlRs.transform);
                        }
                        GameObject ctrlRe = NodeHelper.Find("d_wpnRE", R.gameObject);
                        if (ctrlRe != null)
                        {
                            wt.AddTransform(ctrlRe.transform);
                        }
                        BoxCollider box = R.GetComponentInChildren <BoxCollider>();
                        if (box != null)
                        {
                            box.enabled          = false;
                            box.gameObject.layer = ModelLayer ? LayerMask.NameToLayer("RenderModel") : LayerMask.NameToLayer("Weapon");
                            weaponDamage.Add(box);
                        }
                        else
                        {
                            Debug.LogError("新增武器上找不到碰撞盒[除了暗器火枪飞轮外都应该有碰撞盒]");
                        }

                        if (owner != null)
                        {
                            wt.Init(owner);
                            trail.Add(wt);
                        }
                        else
                        {
                            GameObject.Destroy(wt);
                        }
                    }
                }
            }
            Weapon = item;
        }
        else
        {
            Debug.LogError("if you want equip you must call unequip first if the weapon exist");
        }

        if (owner != null && owner.Stealth)
        {
            MeshRenderer[] mrl = LP.gameObject.GetComponentsInChildren <MeshRenderer>();
            for (int i = 0; i < mrl.Length; i++)
            {
                if (!mrl[i].enabled)
                {
                    continue;
                }
                for (int j = 0; j < mrl[i].materials.Length; j++)
                {
                    mrl[i].materials[j].SetFloat("_Alpha", 0.2f);
                }
            }

            MeshRenderer[] mrr = RP.gameObject.GetComponentsInChildren <MeshRenderer>();
            for (int i = 0; i < mrr.Length; i++)
            {
                if (!mrr[i].enabled)
                {
                    continue;
                }
                for (int j = 0; j < mrr[i].materials.Length; j++)
                {
                    mrr[i].materials[j].SetFloat("_Alpha", 0.2f);
                }
            }
        }
    }
    //GMB文件合并了网格,应该是不用设置位置和旋转了
    public void GenerateWeaponModel(string DesFile, GMBFile fModel, DesFile fIns, bool rightHand = true, float scale = 1.0f, string textureOverrite = "", bool ModelLayer = false)
    {
        if (string.IsNullOrEmpty(DesFile) || fModel == null || fIns == null)
        {
            return;
        }
        string    matIden = DesFile;
        Transform WR      = null;

        if (rightHand)
        {
            if (R != null)
            {
                DestroyImmediate(R);
            }
            R = new GameObject().transform;
            R.gameObject.layer = ModelLayer ? LayerMask.NameToLayer("RenderModel") : LayerMask.NameToLayer("Weapon");
            R.SetParent(RP);
            R.localRotation = Quaternion.identity;
            R.localPosition = Vector3.zero;
            R.localScale    = Vector3.one;
            R.name          = matIden;
            WR = R;
        }
        else
        {
            if (L != null)
            {
                DestroyImmediate(L);
            }
            L = new GameObject().transform;
            L.gameObject.layer = ModelLayer ? LayerMask.NameToLayer("RenderModel") : LayerMask.NameToLayer("Weapon");
            L.SetParent(LP);
            L.localRotation = Quaternion.identity;
            L.localPosition = Vector3.zero;
            L.name          = matIden;
            L.localScale    = Vector3.one;
            WR = L;
        }
        //武器挂点必须在缩放正确后才能到指定的位置
        WR.localScale = Vector3.one;
        Material[] mat = new Material[fModel.TexturesCount];
        Dictionary <int, string> iflMaterials = new Dictionary <int, string>();

        for (int x = 0; x < fModel.TexturesCount; x++)
        {
            mat[x] = null;
            string tex = "";
            if (!string.IsNullOrEmpty(textureOverrite))
            {
                tex = textureOverrite;
            }
            else
            {
                tex = fModel.TexturesNames[x];
            }
            //若不是以ifl结尾的材质,那么要分割文件后缀,若是ifl类型的,则直接是xx.ifl.bytes类型的数据,是OK的
            bool iflMaterial = true;
            if (!tex.ToLower().EndsWith(".ifl"))
            {
                iflMaterial = false;
                int del = tex.LastIndexOf('.');
                if (del != -1)
                {
                    tex = tex.Substring(0, del);
                }
            }

            if (iflMaterial)
            {
                if (!iflMaterials.ContainsKey(x))
                {
                    iflMaterials.Add(x, tex);//记录x号材质是ifl类型的材质,以后会用到,这个序号的材质并且动态更换这个材质的贴图的
                }
            }
            else
            {
                string weaponIden = string.Format("{0}_{1:D2}{2}", matIden, x, textureOverrite);//还要考虑重载贴图也要生成对应的新材质
                mat[x] = Resources.Load <Material>(weaponIden);
                if (mat[x] == null)
                {
                    Texture texture = Resources.Load <Texture>(tex);
                    if (texture == null)
                    {
                        Debug.LogError("texture miss on load gmb:" + tex + " texture name:" + tex);
                    }
                    mat[x] = new Material(ShaderMng.Find("AlphaTexture"));
                    mat[x].SetTexture("_MainTex", texture);
                    mat[x].name = weaponIden;
                    //if (!System.IO.Directory.Exists("Assets/Materials/" + "Weapons" + "/resources/"))
                    //    System.IO.Directory.CreateDirectory("Assets/Materials/" + "Weapons" + "/resources/");
                    //AssetDatabase.CreateAsset(mat[x], "Assets/Materials/" + "Weapons" + "/resources/" + mat[x].name + ".mat");
                    //AssetDatabase.Refresh();
                }
            }
        }

        for (int i = 0; i < fIns.SceneItems.Count; i++)
        {
            GameObject objMesh         = new GameObject();
            bool       addIflComponent = false;
            int        iflParam        = -1;
            int        iflMatIndex     = 0;
            objMesh.name = fIns.SceneItems[i].name;
            objMesh.transform.localRotation = Quaternion.identity;
            objMesh.transform.localPosition = Vector3.zero;
            objMesh.transform.localScale    = Vector3.one;
            bool realObject = false;//是不是正常物体,虚拟体无需设置材质球之类
            if (i < fModel.SceneObjectsCount)
            {
                //for (int j = 0; j < fModel.SceneObjectsCount; j++)
                //{
                //if (fModel.mesh[j].name == objMesh.name)
                {
                    realObject = true;
                    Mesh w = new Mesh();
                    //前者子网格编号,后者 索引缓冲区
                    Dictionary <int, List <int> > tr = new Dictionary <int, List <int> >();
                    List <Vector3> ve  = new List <Vector3>();
                    List <Vector2> uv  = new List <Vector2>();
                    List <Vector3> nor = new List <Vector3>();
                    List <Color>   col = new List <Color>();
                    for (int k = 0; k < fModel.mesh[i].faces.Count; k++)
                    {
                        int key = fModel.mesh[i].faces[k].material;
                        if (tr.ContainsKey(key))
                        {
                            tr[key].Add(fModel.mesh[i].faces[k].triangle[0]);
                            tr[key].Add(fModel.mesh[i].faces[k].triangle[1]);
                            tr[key].Add(fModel.mesh[i].faces[k].triangle[2]);
                        }
                        else
                        {
                            tr.Add(key, new List <int>());
                            tr[key].Add(fModel.mesh[i].faces[k].triangle[0]);
                            tr[key].Add(fModel.mesh[i].faces[k].triangle[1]);
                            tr[key].Add(fModel.mesh[i].faces[k].triangle[2]);
                        }
                    }
                    for (int k = 0; k < fModel.mesh[i].vertices.Count; k++)
                    {
                        ve.Add(fModel.mesh[i].vertices[k].pos);
                        uv.Add(fModel.mesh[i].vertices[k].uv);
                        col.Add(fModel.mesh[i].vertices[k].color);
                        nor.Add(fModel.mesh[i].vertices[k].normal);
                    }
                    w.SetVertices(ve);
                    w.uv           = uv.ToArray();
                    w.subMeshCount = tr.Count;
                    int             ss        = 0;
                    List <Material> targetMat = new List <Material>();
                    foreach (var each in tr)
                    {
                        w.SetIndices(each.Value.ToArray(), MeshTopology.Triangles, ss++);
                        if (each.Key >= 0 && each.Key < fModel.shader.Length)
                        {
                            int materialIndex = fModel.shader[each.Key].TextureArg0;
                            if (materialIndex >= 0 && materialIndex < mat.Length)
                            {
                                if (mat[materialIndex] == null)
                                {
                                    targetMat.Add(new Material(Shader.Find("Unlit/Transparent")));
                                    addIflComponent = true;
                                    iflParam        = materialIndex;
                                    iflMatIndex     = targetMat.Count - 1;
                                }
                                else
                                {
                                    targetMat.Add(mat[materialIndex]);
                                }
                            }
                            else
                            {
                                //占位材质,代表原文件里用到了序号<0的材质(即空材质),这里使用默认材质代替,一般武器加载不会触发这里,但是有极个别情况
                                Material defaults = new Material(Shader.Find("Unlit/Texture"));
                                defaults.name = string.Format("{0}_{1:D2}", objMesh.name, materialIndex);
                                targetMat.Add(defaults);
                            }
                        }
                    }
                    MeshRenderer mr = objMesh.AddComponent <MeshRenderer>();
                    MeshFilter   mf = objMesh.AddComponent <MeshFilter>();
                    mf.mesh         = w;
                    mf.mesh.colors  = col.ToArray();
                    mf.mesh.normals = nor.ToArray();
                    mf.mesh.RecalculateBounds();
                    mf.mesh.RecalculateNormals();

                    mr.materials = targetMat.ToArray();
                    string vis = "";
                    if (fIns.SceneItems[i].ContainsKey("visible", out vis))
                    {
                        if (vis == "0")
                        {
                            mr.enabled = false;
                            BoxCollider box = mr.gameObject.AddComponent <BoxCollider>();
                            objMesh.tag = "weapon";
                            box.enabled = false;
                            weaponDamage.Add(box);
                        }
                    }
                    else
                    if (fIns.SceneItems[i].name.EndsWith("Box"))
                    {
                        BoxCollider box = mr.gameObject.AddComponent <BoxCollider>();
                        box.enabled = false;
                        weaponDamage.Add(box);
                    }
                }
            }

            objMesh.transform.SetParent(WR);
            objMesh.layer = WR.gameObject.layer;
            if (addIflComponent && iflMaterials.ContainsKey(iflParam))
            {
                IFLLoader iflL = objMesh.AddComponent <IFLLoader>();
                iflL.fileNameReadOnly = iflMaterials[iflParam];
                iflL.IFLFile          = Resources.Load <TextAsset>(iflMaterials[iflParam]);
                iflL.matIndex         = iflMatIndex;
                iflL.LoadIFL();
            }

            if (!realObject)
            {
                objMesh.transform.localRotation = fIns.SceneItems[i].quat;
                objMesh.transform.localPosition = fIns.SceneItems[i].pos;
            }
            else
            {
                objMesh.transform.localRotation = Quaternion.identity;
                objMesh.transform.localScale    = Vector3.one;
                objMesh.transform.localPosition = Vector3.zero;
            }
        }

        WR.localScale = Vector3.one * scale;
        //WR.localRotation = new Quaternion(0.7071f, 0, 0, -0.7071f);//挂右手试试不旋转会怎样
        WeaponTrail wt = WR.gameObject.AddComponent <WeaponTrail>();

        if (rightHand)
        {
            //右手
            GameObject ctrlRs = NodeHelper.Find("d_wpnRS", WR.gameObject);
            if (ctrlRs != null)
            {
                wt.AddTransform(ctrlRs.transform);
            }
            GameObject ctrlRe = NodeHelper.Find("d_wpnRE", WR.gameObject);
            if (ctrlRe != null)
            {
                wt.AddTransform(ctrlRe.transform);
            }
        }
        else
        {
            //左手
            GameObject ctrlLs = NodeHelper.Find("d_wpnLS", WR.gameObject);
            if (ctrlLs != null)
            {
                wt.AddTransform(ctrlLs.transform);
            }
            GameObject ctrlLe = NodeHelper.Find("d_wpnLE", WR.gameObject);
            if (ctrlLe != null)
            {
                wt.AddTransform(ctrlLe.transform);
            }
        }
        if (owner != null)
        {
            wt.Init(owner);
            trail.Add(wt);
        }
        else
        {
            GameObject.Destroy(wt);
        }
    }
Exemple #4
0
    //力量感很差,仅仅运行轨迹是一条曲线还是不行的。要有冲击感,要做变速/匀加速按固定轨迹运动,才行
    //因为贝塞尔通过时间计算曲线的某一位置,可以把时间参数模拟加快,做为变量
    //但是怎么去控制
    //LineRenderer line;
    public void LoadAttack(InventoryItem weapon, MeteorUnit target, AttackDes attackdef, MeteorUnit Owner)
    {
        //line = gameObject.AddComponent<LineRenderer>();
        //line.startWidth = 1f;
        //line.endWidth = 1f;
        //line.numPositions = 200;
        if (attackdef != null)
        {
            if (attackdef.PoseIdx == 218)
            {
                //轻力度
                speed = (normal_speed + max_speed) / 2;
            }
            else if (attackdef.PoseIdx == 220)
            {
                //中等力度
                speed = (middle_speed + max_speed) / 2;
            }
            else if (attackdef.PoseIdx == 222)
            {
                //超级力度
                speed = (super_speed + max_speed) / 2;
            }
        }
        owner       = Owner;
        _attack     = attackdef;
        auto_target = target;
        WeaponRoot  = new GameObject().transform;
        WeaponRoot.SetParent(transform);
        WeaponRoot.localPosition    = Vector3.zero;
        WeaponRoot.localScale       = Vector3.one;
        WeaponRoot.localRotation    = Quaternion.Euler(0, 0, 0);
        WeaponRoot.name             = "WeaponRoot";
        WeaponRoot.gameObject.layer = gameObject.layer;
        Weapon = weapon;
        //计算控制点
        spline.SetControlPoint(0, transform.position);
        InitSpline();
        //List<Vector3> veclst = spline.GetEquiDistantPointsOnCurve(200);
        //line.SetPositions(veclst.ToArray());
        totalTime = spline.GetLength() / speed;
        //float a = (max_speed - speed) / totalTime;
        //Debug.LogError("tTotal Init:" + tTotal + " length:" + spline.GetLength() + " speed:" + speed);
        LoadWeapon();
        //增加拖尾
        GameObject trailS = NodeHelper.Find("d_wpnRS", this.gameObject);
        GameObject trailE = NodeHelper.Find("d_wpnRE", this.gameObject);

        Trail = gameObject.AddComponent <WeaponTrail>();
        if (trailS != null)
        {
            Trail.AddTransform(trailS.transform);
        }
        if (trailE != null)
        {
            Trail.AddTransform(trailE.transform);
        }
        Trail.Init(Owner);
        Trail.Open();
        MeshRenderer mr = gameObject.GetComponentInChildren <MeshRenderer>();

        if (mr != null)
        {
            BoxCollider hitBox = mr.gameObject.AddComponent <BoxCollider>();
            hitBox.isTrigger = true;
            hitBox.size      = new Vector3(10, 5, 10);
            if (U3D.showBox)
            {
                BoundsGizmos.Instance.AddCollider(hitBox);
            }
        }
    }