示例#1
0
        public static GameObject CreateDaggerfallMeshGameObject(
            uint modelID,
            Transform parent,
            bool makeStatic = false)
        {
            DaggerfallUnity dfUnity = DaggerfallUnity.Instance;

            // Create gameobject
            GameObject go = new GameObject(string.Format("DaggerfallMesh [ID={0}]", modelID));

            if (parent)
            {
                go.transform.parent = parent;
            }

            // Assign components
            DaggerfallMesh dfMesh       = go.AddComponent <DaggerfallMesh>();
            MeshFilter     meshFilter   = go.GetComponent <MeshFilter>();
            MeshRenderer   meshRenderer = go.GetComponent <MeshRenderer>();

            // Assign mesh and materials
            CachedMaterial[] cachedMaterials;
            int[]            textureKeys;
            bool             hasAnimations;
            Mesh             mesh = dfUnity.MeshReader.GetMesh(
                dfUnity,
                modelID,
                out cachedMaterials,
                out textureKeys,
                out hasAnimations,
                dfUnity.MeshReader.AddMeshTangents,
                dfUnity.MeshReader.AddMeshLightmapUVs);

            // Assign animated materials component if required
            if (hasAnimations)
            {
                AssignAnimateTextureComponent(cachedMaterials, go);
            }

            // Assign mesh and materials
            if (mesh)
            {
                meshFilter.sharedMesh        = mesh;
                meshRenderer.sharedMaterials = GetMaterialArray(cachedMaterials);
                dfMesh.SetDefaultTextures(textureKeys);
            }

            // Assign collider
            if (dfUnity.Option_AddMeshColliders)
            {
                MeshCollider collider = go.AddComponent <MeshCollider>();
                collider.sharedMesh = mesh;
            }

            // Assign static
            if (makeStatic)
            {
                go.isStatic = true;
            }

            return(go);
        }
示例#2
0
        /// <summary>
        /// Adds a single DaggerfallMesh game object to scene.
        /// </summary>
        /// <param name="modelID">ModelID of mesh to add.</param>
        /// <param name="parent">Optional parent of this object.</param>
        /// <param name="makeStatic">Flag to set object static flag.</param>
        /// <param name="useExistingObject">Add mesh to existing object rather than create new.</param>
        /// <param name="ignoreCollider">Force disable collider.</param>
        /// <returns>GameObject.</returns>
        public static GameObject CreateDaggerfallMeshGameObject(
            uint modelID,
            Transform parent,
            bool makeStatic = false,
            GameObject useExistingObject = null,
            bool ignoreCollider          = false)
        {
            DaggerfallUnity dfUnity = DaggerfallUnity.Instance;

            // Create gameobject
            string     name = string.Format("DaggerfallMesh [ID={0}]", modelID);
            GameObject go   = (useExistingObject != null) ? useExistingObject : new GameObject();

            if (parent != null)
            {
                go.transform.parent = parent;
            }
            go.name = name;

            // Add DaggerfallMesh component
            DaggerfallMesh dfMesh = go.GetComponent <DaggerfallMesh>();

            if (dfMesh == null)
            {
                dfMesh = go.AddComponent <DaggerfallMesh>();
            }

            // Get mesh filter and renderer components
            MeshFilter   meshFilter   = go.GetComponent <MeshFilter>();
            MeshRenderer meshRenderer = go.GetComponent <MeshRenderer>();

            // Assign mesh and materials
            CachedMaterial[] cachedMaterials;
            int[]            textureKeys;
            bool             hasAnimations;
            Mesh             mesh = dfUnity.MeshReader.GetMesh(
                dfUnity,
                modelID,
                out cachedMaterials,
                out textureKeys,
                out hasAnimations,
                dfUnity.MeshReader.AddMeshTangents,
                dfUnity.MeshReader.AddMeshLightmapUVs);

            // Assign animated materials component if required
            if (hasAnimations)
            {
                AssignAnimatedMaterialComponent(cachedMaterials, go);
            }

            // Assign mesh and materials
            if (mesh)
            {
                meshFilter.sharedMesh        = mesh;
                meshRenderer.sharedMaterials = GetMaterialArray(cachedMaterials);
                dfMesh.SetDefaultTextures(textureKeys);
            }

            // Assign mesh to collider
            if (dfUnity.Option_AddMeshColliders && !ignoreCollider)
            {
                MeshCollider collider = go.GetComponent <MeshCollider>();
                if (collider == null)
                {
                    collider = go.AddComponent <MeshCollider>();
                }
                collider.sharedMesh = mesh;
            }

            // Assign static
            if (makeStatic)
            {
                go.isStatic = true;
            }

            return(go);
        }