Exemplo n.º 1
0
        private void Bake()
        {
            if (CheckForDynamicMeshes)
            {
                Debug.LogError("Since this object is flag to check for dynamic objects, it cannot be baked.");
                return;
            }

            var tempFilters         = GetComponentsInChildren <MeshFilter>(true);
            var filtersToIgnoreList = new List <int>();

            if (IgnoredObjects != null)
            {
                for (int i = 0; i < tempFilters.Length; i++)
                {
                    for (int j = 0; j < IgnoredObjects.Length; j++)
                    {
                        if (tempFilters[i].gameObject == IgnoredObjects[j])
                        {
                            filtersToIgnoreList.Add(i);
                            break;
                        }
                    }
                }
            }

            var listOfFilters = new List <MeshFilter>();

            for (int i = 0; i < tempFilters.Length; i++)
            {
                if (filtersToIgnoreList.Contains(i))
                {
                    continue;
                }

                listOfFilters.Add(tempFilters[i]);
            }

            MeshFilters       = listOfFilters.ToArray();
            MeshFiltersLength = MeshFilters.Length;
            Colors            = new VertexLitObjectColors[MeshFiltersLength];
            for (var i = 0; i < MeshFiltersLength; i++)
            {
                var color      = new VertexLitObjectColors();
                var meshFilter = MeshFilters[i];

                color.ColorsLength = meshFilter.sharedMesh.vertexCount;
                color.Colors       = new Color32[color.ColorsLength];

                Colors[i] = color;
            }

            IsSetup = true;
        }
Exemplo n.º 2
0
        /// <summary>
        /// Should ideally be called when the object is first instantiated, that is, when the pool containing this object is loaded or when the scene is loaded.
        /// Unity will call this again on Enable, but checks will ensure it does not run twice. It is bad to run this 'OnEnable' because it caches a lot of
        /// references, and if possible all this should be done when the new scene is loaded and the pool of this object is warmed up, to avoid spikes in
        /// performance.
        /// </summary>
        public void Setup(bool forced)
        {
            if (IsSetup && !forced)
            {
                return;
            }

            if (!CheckForDynamicMeshes && !forced)
            {
                if (MeshFilters == null || MeshFilters.Length == 0)
                {
                    Debug.LogError("This object should have been baked already!!", gameObject);
                }

                return;
            }

            var dynamicIgnoredObjects = GetComponentsInChildren <VertexDynamicUnlitObject>();
            var filters = GetComponentsInChildren <MeshFilter>();

            var filtersLength        = filters.Length;
            var dynamicObjectsLength = dynamicIgnoredObjects.Length;
            var ignoredObjectsLength = IgnoredObjects.Length;

            var ignoredObjectsCount   = 0;
            var indexesToIgnore       = new int[dynamicObjectsLength + ignoredObjectsLength];
            var indexesToIgnoreLength = dynamicObjectsLength + ignoredObjectsLength;

            for (int i = 0; i < filtersLength; i++)
            {
                var filter = filters[i];

                var foundObject = false;
                for (int j = 0; j < dynamicObjectsLength; j++)
                {
                    if (filter.gameObject.GetInstanceID() == dynamicIgnoredObjects[j].gameObject.GetInstanceID())
                    {
                        indexesToIgnore[ignoredObjectsCount] = i;
                        ignoredObjectsCount++;
                        foundObject = true;
                        break;
                    }
                }

                if (foundObject || IgnoredObjects == null)
                {
                    continue;
                }

                for (int j = 0; j < ignoredObjectsLength; j++)
                {
                    if (filter.gameObject.GetInstanceID() == IgnoredObjects[j].gameObject.GetInstanceID())
                    {
                        indexesToIgnore[ignoredObjectsCount] = i;
                        ignoredObjectsCount++;
                        break;
                    }
                }
            }

            MeshFilters = new MeshFilter[filtersLength - indexesToIgnoreLength];

            var countedIgnoredObjects = 0;

            for (int i = 0; i < filtersLength; i++)
            {
                var ignoreItem = false;
                for (int j = 0; j < indexesToIgnoreLength; j++)
                {
                    if (i == indexesToIgnore[j])
                    {
                        ignoreItem = true;
                        break;
                    }
                }

                if (ignoreItem)
                {
                    countedIgnoredObjects++;
                    continue;
                }

                MeshFilters[i - countedIgnoredObjects] = filters[i];
            }

            MeshFiltersLength = MeshFilters.Length;
            Colors            = new VertexLitObjectColors[MeshFiltersLength];
            for (var i = 0; i < MeshFiltersLength; i++)
            {
                var color      = new VertexLitObjectColors();
                var meshFilter = MeshFilters[i];

                color.ColorsLength = meshFilter.sharedMesh.vertexCount;
                color.Colors       = new Color32[color.ColorsLength];

                Colors[i] = color;
            }

            IsSetup = true;
        }