示例#1
0
        private void SwapMeshes(VehicleInfo template, PropInfo target)
        {
            var vehicle   = Util.InstantiateVehicleCopy(template);
            var vehicleGO = vehicle.gameObject;
            var propGO    = target.gameObject;

            var vehicleMf = vehicleGO.GetComponent <MeshFilter>();
            var propMf    = propGO.GetComponent <MeshFilter>();

            // set mesh
            vehicleMf.sharedMesh = vehicleMf.mesh;
            propMf.mesh          = vehicleMf.sharedMesh;
            propMf.sharedMesh    = propMf.mesh;

            // set material
            var renderer    = propGO.GetComponent <Renderer>();
            var vehRenderer = vehicleGO.GetComponent <Renderer>();

            if (renderer != null)
            {
                var shader         = renderer.sharedMaterial.shader;
                var shaderKeyWords = renderer.sharedMaterial.shaderKeywords;

                var material = vehRenderer.sharedMaterial;
                CleanACIMap(material);
                renderer.sharedMaterial = material;

                renderer.sharedMaterial.shader         = shader;
                renderer.sharedMaterial.shaderKeywords = shaderKeyWords;
            }

            // set lod
            if (vehicle.m_lodObject != null)
            {
                var lodRenderer    = target.m_lodObject.GetComponent <Renderer>();
                var lodVehRenderer = vehicle.m_lodObject.GetComponent <Renderer>();

                var material = lodVehRenderer.sharedMaterial;
                CleanACIMap(material);
                lodVehRenderer.sharedMaterial = material;

                lodVehRenderer.sharedMaterial.shader         = lodRenderer.sharedMaterial.shader;
                lodVehRenderer.sharedMaterial.shaderKeywords = lodRenderer.sharedMaterial.shaderKeywords;

                target.m_lodObject = vehicle.m_lodObject;
            }

            // Recalculate generated info
            target.CalculateGeneratedInfo();
            target.m_mesh = null;       // Required to get InitializePrefab to do its thing
            target.m_prefabInitialized = false;
            target.InitializePrefab();
            target.m_prefabInitialized = true;

            Destroy(vehicleGO);
        }
        /// <summary>
        /// Creates and instantiates a copy of a PropInfo
        /// </summary>
        public static PropInfo InstantiatePropCopy(PropInfo template)
        {
            if (template == null)
            {
                return(null);
            }

            PropInfo copyInfo = GameObject.Instantiate(template);

            copyInfo.name = template.name;
            copyInfo.gameObject.SetActive(false);

            // Create generated info
            copyInfo.m_generatedInfo            = ScriptableObject.CreateInstance <PropInfoGen>();
            copyInfo.m_generatedInfo.name       = copyInfo.name;
            copyInfo.m_generatedInfo.m_propInfo = copyInfo;
            copyInfo.CalculateGeneratedInfo();

            // Create LOD object
            if (template.m_lodObject != null)
            {
                GameObject copyLod = GameObject.Instantiate(template.m_lodObject);
                copyLod.SetActive(false);
                // Set sharedmaterial
                Renderer lodRenderer = copyLod.GetComponent <Renderer>();
                if (lodRenderer != null)
                {
                    lodRenderer.sharedMaterial = lodRenderer.material;
                }
                copyInfo.m_lodObject = copyLod;
            }

            // Set sharedmaterial
            Renderer r = copyInfo.GetComponent <Renderer>();

            if (r != null)
            {
                r.sharedMaterial = r.material;
            }
            copyInfo.InitializePrefab();
            copyInfo.m_prefabInitialized = true;
            return(copyInfo);
        }