void Awake()
    {
        GameObject go = new GameObject();

        tk2dStaticSpriteBatcher batcher = go.AddComponent<tk2dStaticSpriteBatcher>();
        batcher.batchedSprites = new tk2dBatchedSprite[20];

        for (int i = 0; i < batcher.batchedSprites.Length; ++i) {
            tk2dBatchedSprite bs = new tk2dBatchedSprite();

            // assign sprite collection and sprite Id for this batched sprite
            bs.spriteCollection = spriteCollection;
            bs.spriteId = spriteCollection.GetSpriteIdByName("crate");

            Vector3 pos = new Vector3((i - batcher.batchedSprites.Length / 2) * 0.1f, Random.value * 0.2f, 0);

            // Assign the relative matrix. Use this in place of bs.position
            bs.relativeMatrix.SetTRS(pos, Quaternion.identity, Vector3.one);

            batcher.batchedSprites[i] = bs;
        }

        // Don't create colliders when you don't need them. It is very expensive to
        // generate colliders at runtime.
        batcher.SetFlag( tk2dStaticSpriteBatcher.Flags.GenerateCollider, false );

        batcher.Build();
    }
Esempio n. 2
0
    // This is used by other parts of code
    public static void RestoreBatchedSprite(GameObject go, tk2dBatchedSprite bs)
    {
        tk2dBaseSprite baseSprite = null;

        switch (bs.type)
        {
        case tk2dBatchedSprite.Type.EmptyGameObject:
        {
            break;
        }

        case tk2dBatchedSprite.Type.Sprite:
        {
            tk2dSprite s = tk2dBaseSprite.AddComponent <tk2dSprite>(go, bs.spriteCollection, bs.spriteId);
            baseSprite = s;
            break;
        }

        case tk2dBatchedSprite.Type.TiledSprite:
        {
            tk2dTiledSprite s = tk2dBaseSprite.AddComponent <tk2dTiledSprite>(go, bs.spriteCollection, bs.spriteId);
            baseSprite          = s;
            s.dimensions        = bs.Dimensions;
            s.anchor            = bs.anchor;
            s.CreateBoxCollider = bs.CheckFlag(tk2dBatchedSprite.Flags.Sprite_CreateBoxCollider);
            RestoreBoxColliderSettings(s.gameObject, bs.BoxColliderOffsetZ, bs.BoxColliderExtentZ);
            break;
        }

        case tk2dBatchedSprite.Type.SlicedSprite:
        {
            tk2dSlicedSprite s = tk2dBaseSprite.AddComponent <tk2dSlicedSprite>(go, bs.spriteCollection, bs.spriteId);
            baseSprite   = s;
            s.dimensions = bs.Dimensions;
            s.anchor     = bs.anchor;

            s.BorderOnly = bs.CheckFlag(tk2dBatchedSprite.Flags.SlicedSprite_BorderOnly);
            s.SetBorder(bs.SlicedSpriteBorderBottomLeft.x, bs.SlicedSpriteBorderBottomLeft.y, bs.SlicedSpriteBorderTopRight.x, bs.SlicedSpriteBorderTopRight.y);

            s.CreateBoxCollider = bs.CheckFlag(tk2dBatchedSprite.Flags.Sprite_CreateBoxCollider);
            RestoreBoxColliderSettings(s.gameObject, bs.BoxColliderOffsetZ, bs.BoxColliderExtentZ);
            break;
        }

        case tk2dBatchedSprite.Type.ClippedSprite:
        {
            tk2dClippedSprite s = tk2dBaseSprite.AddComponent <tk2dClippedSprite>(go, bs.spriteCollection, bs.spriteId);
            baseSprite       = s;
            s.clipBottomLeft = bs.ClippedSpriteRegionBottomLeft;
            s.clipTopRight   = bs.ClippedSpriteRegionTopRight;

            s.CreateBoxCollider = bs.CheckFlag(tk2dBatchedSprite.Flags.Sprite_CreateBoxCollider);
            RestoreBoxColliderSettings(s.gameObject, bs.BoxColliderOffsetZ, bs.BoxColliderExtentZ);
            break;
        }
        }
        baseSprite.scale = bs.baseScale;
        baseSprite.color = bs.color;
    }
Esempio n. 3
0
    public static void FillBatchedSprite(tk2dBatchedSprite bs, GameObject go)
    {
        tk2dSprite        srcSprite        = go.transform.GetComponent <tk2dSprite>();
        tk2dTiledSprite   srcTiledSprite   = go.transform.GetComponent <tk2dTiledSprite>();
        tk2dSlicedSprite  srcSlicedSprite  = go.transform.GetComponent <tk2dSlicedSprite>();
        tk2dClippedSprite srcClippedSprite = go.transform.GetComponent <tk2dClippedSprite>();

        tk2dBaseSprite baseSprite = go.GetComponent <tk2dBaseSprite>();

        bs.spriteId         = baseSprite.spriteId;
        bs.spriteCollection = baseSprite.Collection;
        bs.baseScale        = baseSprite.scale;
        bs.color            = baseSprite.color;
        bs.renderLayer      = baseSprite.SortingOrder;
#if !STRIP_PHYSICS_3D
        if (baseSprite.boxCollider != null)
        {
            bs.BoxColliderOffsetZ = baseSprite.boxCollider.center.z;
            bs.BoxColliderExtentZ = baseSprite.boxCollider.size.z * 0.5f;
        }
        else
#endif
        {
            bs.BoxColliderOffsetZ = 0.0f;
            bs.BoxColliderExtentZ = 1.0f;
        }

        if (srcSprite)
        {
            bs.type = tk2dBatchedSprite.Type.Sprite;
        }
        else if (srcTiledSprite)
        {
            bs.type       = tk2dBatchedSprite.Type.TiledSprite;
            bs.Dimensions = srcTiledSprite.dimensions;
            bs.anchor     = srcTiledSprite.anchor;
            bs.SetFlag(tk2dBatchedSprite.Flags.Sprite_CreateBoxCollider, srcTiledSprite.CreateBoxCollider);
        }
        else if (srcSlicedSprite)
        {
            bs.type       = tk2dBatchedSprite.Type.SlicedSprite;
            bs.Dimensions = srcSlicedSprite.dimensions;
            bs.anchor     = srcSlicedSprite.anchor;
            bs.SetFlag(tk2dBatchedSprite.Flags.Sprite_CreateBoxCollider, srcSlicedSprite.CreateBoxCollider);
            bs.SetFlag(tk2dBatchedSprite.Flags.SlicedSprite_BorderOnly, srcSlicedSprite.BorderOnly);
            bs.SlicedSpriteBorderBottomLeft = new Vector2(srcSlicedSprite.borderLeft, srcSlicedSprite.borderBottom);
            bs.SlicedSpriteBorderTopRight   = new Vector2(srcSlicedSprite.borderRight, srcSlicedSprite.borderTop);
        }
        else if (srcClippedSprite)
        {
            bs.type = tk2dBatchedSprite.Type.ClippedSprite;
            bs.ClippedSpriteRegionBottomLeft = srcClippedSprite.clipBottomLeft;
            bs.ClippedSpriteRegionTopRight   = srcClippedSprite.clipTopRight;
            bs.SetFlag(tk2dBatchedSprite.Flags.Sprite_CreateBoxCollider, srcClippedSprite.CreateBoxCollider);
        }
    }
Esempio n. 4
0
    Material GetMaterial(tk2dBatchedSprite bs)
    {
        switch (bs.type)
        {
        case tk2dBatchedSprite.Type.EmptyGameObject: return(null);

        case tk2dBatchedSprite.Type.TextMesh: return(allTextMeshData[bs.xRefId].font.materialInst);

        default: return(bs.GetSpriteDefinition().materialInst);
        }
    }
Esempio n. 5
0
    static void ConvertSpriteType(GameObject go, System.Type targetType)
    {
        tk2dBaseSprite spr = go.GetComponent <tk2dBaseSprite>();

        System.Type sourceType = spr.GetType();

        if (sourceType != targetType)
        {
            tk2dBatchedSprite batchedSprite = new tk2dBatchedSprite();
            tk2dStaticSpriteBatcherEditor.FillBatchedSprite(batchedSprite, go);
            if (targetType == typeof(tk2dSprite))
            {
                batchedSprite.type = tk2dBatchedSprite.Type.Sprite;
            }
            else if (targetType == typeof(tk2dTiledSprite))
            {
                batchedSprite.type = tk2dBatchedSprite.Type.TiledSprite;
            }
            else if (targetType == typeof(tk2dSlicedSprite))
            {
                batchedSprite.type = tk2dBatchedSprite.Type.SlicedSprite;
            }
            else if (targetType == typeof(tk2dClippedSprite))
            {
                batchedSprite.type = tk2dBatchedSprite.Type.ClippedSprite;
            }

            Object.DestroyImmediate(spr, true);

            bool sourceHasDimensions = sourceType == typeof(tk2dSlicedSprite) || sourceType == typeof(tk2dTiledSprite);
            bool targetHasDimensions = targetType == typeof(tk2dSlicedSprite) || targetType == typeof(tk2dTiledSprite);

            // Some minor fixups
            if (!sourceHasDimensions && targetHasDimensions)
            {
                batchedSprite.Dimensions = new Vector2(100, 100);
            }
            if (targetType == typeof(tk2dClippedSprite))
            {
                batchedSprite.ClippedSpriteRegionBottomLeft = Vector2.zero;
                batchedSprite.ClippedSpriteRegionTopRight   = Vector2.one;
            }
            if (targetType == typeof(tk2dSlicedSprite))
            {
                batchedSprite.SlicedSpriteBorderBottomLeft = new Vector2(0.1f, 0.1f);
                batchedSprite.SlicedSpriteBorderTopRight   = new Vector2(0.1f, 0.1f);
            }

            tk2dStaticSpriteBatcherEditor.RestoreBatchedSprite(go, batchedSprite);
        }
    }
    static void ConvertSpriteType(GameObject go, System.Type targetType)
    {
        tk2dBaseSprite spr = go.GetComponent <tk2dBaseSprite>();

        System.Type sourceType = spr.GetType();

        if (sourceType != targetType)
        {
            tk2dBatchedSprite batchedSprite = new tk2dBatchedSprite();
            tk2dStaticSpriteBatcherEditor.FillBatchedSprite(batchedSprite, go);
            if (targetType == typeof(tk2dSprite))
            {
                batchedSprite.type = tk2dBatchedSprite.Type.Sprite;
            }
            else if (targetType == typeof(tk2dTiledSprite))
            {
                batchedSprite.type = tk2dBatchedSprite.Type.TiledSprite;
            }
            else if (targetType == typeof(tk2dSlicedSprite))
            {
                batchedSprite.type = tk2dBatchedSprite.Type.SlicedSprite;
            }
            else if (targetType == typeof(tk2dClippedSprite))
            {
                batchedSprite.type = tk2dBatchedSprite.Type.ClippedSprite;
            }

#if (UNITY_3_5 || UNITY_4_0 || UNITY_4_0_1 || UNITY_4_1 || UNITY_4_2)
            if (spr.collider != null)
            {
                Object.DestroyImmediate(spr.collider);
            }
            Object.DestroyImmediate(spr, true);
#else
            {
                Collider[] colliders = spr.GetComponents <Collider>();
                foreach (Collider c in colliders)
                {
                    Undo.DestroyObjectImmediate(c);
                }
                Collider2D[] collider2Ds = spr.GetComponents <Collider2D>();
                foreach (Collider2D c in collider2Ds)
                {
                    Undo.DestroyObjectImmediate(c);
                }
            }
            Undo.DestroyObjectImmediate(spr);
#endif

            bool sourceHasDimensions = sourceType == typeof(tk2dSlicedSprite) || sourceType == typeof(tk2dTiledSprite);
            bool targetHasDimensions = targetType == typeof(tk2dSlicedSprite) || targetType == typeof(tk2dTiledSprite);

            // Some minor fixups
            if (!sourceHasDimensions && targetHasDimensions)
            {
                batchedSprite.Dimensions = new Vector2(100, 100);
            }
            if (targetType == typeof(tk2dClippedSprite))
            {
                batchedSprite.ClippedSpriteRegionBottomLeft = Vector2.zero;
                batchedSprite.ClippedSpriteRegionTopRight   = Vector2.one;
            }
            if (targetType == typeof(tk2dSlicedSprite))
            {
                batchedSprite.SlicedSpriteBorderBottomLeft = new Vector2(0.1f, 0.1f);
                batchedSprite.SlicedSpriteBorderTopRight   = new Vector2(0.1f, 0.1f);
            }

            tk2dStaticSpriteBatcherEditor.RestoreBatchedSprite(go, batchedSprite);
#if !(UNITY_3_5 || UNITY_4_0 || UNITY_4_0_1 || UNITY_4_1 || UNITY_4_2)
            {
                tk2dBaseSprite tmpSprite = go.GetComponent <tk2dBaseSprite>();
                if (tmpSprite != null)
                {
                    Undo.RegisterCreatedObjectUndo(tmpSprite, "Convert Sprite Type");
                }
            }
#endif
        }
    }
	// This is used by other parts of code
	public static void RestoreBatchedSprite(GameObject go, tk2dBatchedSprite bs) {
		tk2dBaseSprite baseSprite = null;
		switch (bs.type) {
			case tk2dBatchedSprite.Type.EmptyGameObject:
				{
					break;
				}
			case tk2dBatchedSprite.Type.Sprite:
				{
					tk2dSprite s = tk2dBaseSprite.AddComponent<tk2dSprite>(go, bs.spriteCollection, bs.spriteId);
					baseSprite = s;
					break;
				}
			case tk2dBatchedSprite.Type.TiledSprite:
				{
					tk2dTiledSprite s = tk2dBaseSprite.AddComponent<tk2dTiledSprite>(go, bs.spriteCollection, bs.spriteId);
					baseSprite = s;
					s.dimensions = bs.Dimensions;
					s.anchor = bs.anchor;
					s.CreateBoxCollider = bs.CheckFlag(tk2dBatchedSprite.Flags.Sprite_CreateBoxCollider);
					RestoreBoxColliderSettings(s.gameObject, bs.BoxColliderOffsetZ, bs.BoxColliderExtentZ);
					break;
				}
			case tk2dBatchedSprite.Type.SlicedSprite:
				{
					tk2dSlicedSprite s = tk2dBaseSprite.AddComponent<tk2dSlicedSprite>(go, bs.spriteCollection, bs.spriteId);
					baseSprite = s;
					s.dimensions = bs.Dimensions;
					s.anchor = bs.anchor;

					s.BorderOnly = bs.CheckFlag(tk2dBatchedSprite.Flags.SlicedSprite_BorderOnly);
					s.SetBorder(bs.SlicedSpriteBorderBottomLeft.x, bs.SlicedSpriteBorderBottomLeft.y, bs.SlicedSpriteBorderTopRight.x, bs.SlicedSpriteBorderTopRight.y);

					s.CreateBoxCollider = bs.CheckFlag(tk2dBatchedSprite.Flags.Sprite_CreateBoxCollider);
					RestoreBoxColliderSettings(s.gameObject, bs.BoxColliderOffsetZ, bs.BoxColliderExtentZ);
					break;
				}
			case tk2dBatchedSprite.Type.ClippedSprite:
				{
					tk2dClippedSprite s = tk2dBaseSprite.AddComponent<tk2dClippedSprite>(go, bs.spriteCollection, bs.spriteId);
					baseSprite = s;
					s.clipBottomLeft = bs.ClippedSpriteRegionBottomLeft;
					s.clipTopRight = bs.ClippedSpriteRegionTopRight;

					s.CreateBoxCollider = bs.CheckFlag(tk2dBatchedSprite.Flags.Sprite_CreateBoxCollider);
					RestoreBoxColliderSettings(s.gameObject, bs.BoxColliderOffsetZ, bs.BoxColliderExtentZ);
					break;
				}
		}
		baseSprite.scale = bs.baseScale;
		baseSprite.color = bs.color;		
	}
	public static void FillBatchedSprite(tk2dBatchedSprite bs, GameObject go) {
		tk2dSprite srcSprite = go.transform.GetComponent<tk2dSprite>();
		tk2dTiledSprite srcTiledSprite = go.transform.GetComponent<tk2dTiledSprite>();
		tk2dSlicedSprite srcSlicedSprite = go.transform.GetComponent<tk2dSlicedSprite>();
		tk2dClippedSprite srcClippedSprite = go.transform.GetComponent<tk2dClippedSprite>();

		tk2dBaseSprite baseSprite = go.GetComponent<tk2dBaseSprite>();
		bs.spriteId = baseSprite.spriteId;
		bs.spriteCollection = baseSprite.Collection;
		bs.baseScale = baseSprite.scale;
		bs.color = baseSprite.color;
		if (baseSprite.boxCollider != null)
		{
			bs.BoxColliderOffsetZ = baseSprite.boxCollider.center.z;
			bs.BoxColliderExtentZ = baseSprite.boxCollider.size.z * 0.5f;
		}
		else {
			bs.BoxColliderOffsetZ = 0.0f;
			bs.BoxColliderExtentZ = 1.0f;
		}

		if (srcSprite) {
			bs.type = tk2dBatchedSprite.Type.Sprite;
		}
		else if (srcTiledSprite) {
			bs.type = tk2dBatchedSprite.Type.TiledSprite;
			bs.Dimensions = srcTiledSprite.dimensions;
			bs.anchor = srcTiledSprite.anchor;
			bs.SetFlag(tk2dBatchedSprite.Flags.Sprite_CreateBoxCollider, srcTiledSprite.CreateBoxCollider);
		}
		else if (srcSlicedSprite) {
			bs.type = tk2dBatchedSprite.Type.SlicedSprite;
			bs.Dimensions = srcSlicedSprite.dimensions;
			bs.anchor = srcSlicedSprite.anchor;
			bs.SetFlag(tk2dBatchedSprite.Flags.Sprite_CreateBoxCollider, srcSlicedSprite.CreateBoxCollider);
			bs.SetFlag(tk2dBatchedSprite.Flags.SlicedSprite_BorderOnly, srcSlicedSprite.BorderOnly);
			bs.SlicedSpriteBorderBottomLeft = new Vector2(srcSlicedSprite.borderLeft, srcSlicedSprite.borderBottom);
			bs.SlicedSpriteBorderTopRight = new Vector2(srcSlicedSprite.borderRight, srcSlicedSprite.borderTop);
		}
		else if (srcClippedSprite) {
			bs.type = tk2dBatchedSprite.Type.ClippedSprite;
			bs.ClippedSpriteRegionBottomLeft = srcClippedSprite.clipBottomLeft;
			bs.ClippedSpriteRegionTopRight = srcClippedSprite.clipTopRight;
			bs.SetFlag(tk2dBatchedSprite.Flags.Sprite_CreateBoxCollider, srcClippedSprite.CreateBoxCollider);
		}
	}
	void DrawEditorGUI()
	{
		if (GUILayout.Button("Commit"))
		{
			// Select all children, EXCLUDING self
			Transform[] allTransforms = batcher.transform.GetComponentsInChildren<Transform>();
			allTransforms = (from t in allTransforms where t != batcher.transform select t).ToArray();
			
			// sort sprites, smaller to larger z
			if (batcher.CheckFlag(tk2dStaticSpriteBatcher.Flags.SortToCamera)) {
				tk2dCamera tk2dCam = tk2dCamera.CameraForLayer( batcher.gameObject.layer );
				Camera cam = tk2dCam ? tk2dCam.camera : Camera.main;
				allTransforms = (from t in allTransforms orderby cam.WorldToScreenPoint((t.renderer != null) ? t.renderer.bounds.center : t.position).z descending select t).ToArray();
			}
			else {
				allTransforms = (from t in allTransforms orderby t.position.z descending select t).ToArray();
			}
			
			// and within the z sort by material
			if (allTransforms.Length == 0)
			{
				EditorUtility.DisplayDialog("StaticSpriteBatcher", "Error: No child objects found", "Ok");
				return;
			}
		
			Dictionary<Transform, int> batchedSpriteLookup = new Dictionary<Transform, int>();
			batchedSpriteLookup[batcher.transform] = -1;

			Matrix4x4 batcherWorldToLocal = batcher.transform.worldToLocalMatrix;
			
			batcher.spriteCollection = null;
			batcher.batchedSprites = new tk2dBatchedSprite[allTransforms.Length];
			List<tk2dTextMeshData> allTextMeshData = new List<tk2dTextMeshData>();

			int currBatchedSprite = 0;
			foreach (var t in allTransforms)
			{
				tk2dBaseSprite baseSprite = t.GetComponent<tk2dBaseSprite>();
				tk2dTextMesh textmesh = t.GetComponent<tk2dTextMesh>();

				tk2dBatchedSprite bs = new tk2dBatchedSprite();
				bs.name = t.gameObject.name;
				bs.position = t.localPosition;
				bs.rotation = t.localRotation;
				bs.relativeMatrix = batcherWorldToLocal * t.localToWorldMatrix;

				if (baseSprite)
				{
					bs.baseScale = Vector3.one;
					bs.localScale = new Vector3(t.localScale.x * baseSprite.scale.x, t.localScale.y * baseSprite.scale.y, t.localScale.z * baseSprite.scale.z);
					FillBatchedSprite(bs, t.gameObject);

					// temp redundant - just incase batcher expects to point to a valid one, somewhere we've missed
					batcher.spriteCollection = baseSprite.Collection;
				}
				else if (textmesh)
				{
					bs.spriteCollection = null;

					bs.type = tk2dBatchedSprite.Type.TextMesh;
					bs.color = textmesh.color;
					bs.baseScale = textmesh.scale;
					bs.localScale = new Vector3(t.localScale.x * textmesh.scale.x, t.localScale.y * textmesh.scale.y, t.localScale.z * textmesh.scale.z);
					bs.FormattedText = textmesh.FormattedText;

					tk2dTextMeshData tmd = new tk2dTextMeshData();
					tmd.font = textmesh.font;
					tmd.text = textmesh.text;
					tmd.color = textmesh.color;
					tmd.color2 = textmesh.color2;
					tmd.useGradient = textmesh.useGradient;
					tmd.textureGradient = textmesh.textureGradient;
					tmd.anchor = textmesh.anchor;
					tmd.kerning = textmesh.kerning;
					tmd.maxChars = textmesh.maxChars;
					tmd.inlineStyling = textmesh.inlineStyling;
					tmd.formatting = textmesh.formatting;
					tmd.wordWrapWidth = textmesh.wordWrapWidth;
					tmd.spacing = textmesh.Spacing;
					tmd.lineSpacing = textmesh.LineSpacing;

					bs.xRefId = allTextMeshData.Count;
					allTextMeshData.Add(tmd);
				}
				else
				{
					// Empty GameObject
					bs.spriteId = -1;
					bs.baseScale = Vector3.one;
					bs.localScale = t.localScale;
					bs.type = tk2dBatchedSprite.Type.EmptyGameObject;
				}

				
				batchedSpriteLookup[t] = currBatchedSprite;
				batcher.batchedSprites[currBatchedSprite++] = bs;
			}
			batcher.allTextMeshData = allTextMeshData.ToArray();
			
			int idx = 0;
			foreach (var t in allTransforms)
			{
				var bs = batcher.batchedSprites[idx];

				bs.parentId = batchedSpriteLookup[t.parent];
				t.parent = batcher.transform; // unparent
				++idx;
			}
			
			Transform[] directChildren = (from t in allTransforms where t.parent == batcher.transform select t).ToArray();
			foreach (var t in directChildren)
			{
				GameObject.DestroyImmediate(t.gameObject);
			}
			
			Vector3 inverseScale = new Vector3(1.0f / batcher.scale.x, 1.0f / batcher.scale.y, 1.0f / batcher.scale.z);
			batcher.transform.localScale = Vector3.Scale( batcher.transform.localScale, inverseScale );
			batcher.Build();
			EditorUtility.SetDirty(target);
		}
	}
    void DrawEditorGUI()
    {
        if (GUILayout.Button("Commit"))
        {
            // Select all children, EXCLUDING self
            Transform[] allTransforms = batcher.transform.GetComponentsInChildren <Transform>();

            List <Transform> tempAllTransforms = new List <Transform>();
            foreach (Transform t in allTransforms)
            {
                if (t != batcher.transform)
                {
                    tempAllTransforms.Add(t);
                }
            }

            allTransforms = tempAllTransforms.ToArray();


#if !(UNITY_3_5 || UNITY_4_0 || UNITY_4_0_1 || UNITY_4_1 || UNITY_4_2)
            Renderer[] allRenderers = batcher.transform.GetComponentsInChildren <Renderer>();

            List <Renderer> tempAllRenderers = new List <Renderer>();
            foreach (Renderer r in allRenderers)
            {
                if (r != batcher.GetComponent <Renderer>())
                {
                    tempAllRenderers.Add(r);
                }
            }

            allRenderers = tempAllRenderers.ToArray();


            if (allRenderers.Length > 0)
            {
                string sortingLayerName = allRenderers[0].sortingLayerName;
                int    sortingOrder     = allRenderers[0].sortingOrder;
                foreach (Renderer r in allRenderers)
                {
                    if (sortingLayerName != r.sortingLayerName ||
                        sortingOrder != r.sortingOrder)
                    {
                        EditorUtility.DisplayDialog("StaticSpriteBatcher", "Error: Child objects use different sorting layer names and/or sorting orders.\n\nOnly one sorting layer and order is permitted in a static sprite batcher.", "Ok");
                        return;
                    }
                }
            }
#endif

            List <KeyValuePair <Transform, float> > tempList = new List <KeyValuePair <Transform, float> >();

            // sort sprites, smaller to larger z
            if (batcher.CheckFlag(tk2dStaticSpriteBatcher.Flags.SortToCamera))
            {
                tk2dCamera tk2dCam = tk2dCamera.CameraForLayer(batcher.gameObject.layer);
                Camera     cam     = tk2dCam ? tk2dCam.GetComponent <Camera>() : Camera.main;

                foreach (Transform t in allTransforms)
                {
                    tempList.Add(new KeyValuePair <Transform, float>(t, cam.WorldToScreenPoint((t.GetComponent <Renderer>() != null) ? t.GetComponent <Renderer>().bounds.center : t.position).z));
                }
            }
            else
            {
                foreach (Transform t in allTransforms)
                {
                    tempList.Add(new KeyValuePair <Transform, float>(t, ((t.GetComponent <Renderer>() != null) ? t.GetComponent <Renderer>().bounds.center : t.position).z));
                }
            }

            tempList.Sort((a, b) => a.Value.CompareTo(b.Value));

            List <Transform> tr = new List <Transform>();
            foreach (KeyValuePair <Transform, float> pair in tempList)
            {
                tr.Add(pair.Key);
            }

            allTransforms = tr.ToArray();

            // and within the z sort by material
            if (allTransforms.Length == 0)
            {
                EditorUtility.DisplayDialog("StaticSpriteBatcher", "Error: No child objects found", "Ok");
                return;
            }


#if !(UNITY_3_5 || UNITY_4_0 || UNITY_4_0_1 || UNITY_4_1 || UNITY_4_2)
            MeshCollider[]      childMeshColliders      = GetComponentsInChildrenExcludeSelf <MeshCollider>(batcher.transform);
            BoxCollider[]       childBoxColliders       = GetComponentsInChildrenExcludeSelf <BoxCollider>(batcher.transform);
            BoxCollider2D[]     childBoxCollider2Ds     = GetComponentsInChildrenExcludeSelf <BoxCollider2D>(batcher.transform);
            EdgeCollider2D[]    childEdgeCollider2Ds    = GetComponentsInChildrenExcludeSelf <EdgeCollider2D>(batcher.transform);
            PolygonCollider2D[] childPolygonCollider2Ds = GetComponentsInChildrenExcludeSelf <PolygonCollider2D>(batcher.transform);

            if ((childMeshColliders.Length > 0 || childBoxColliders.Length > 0) && (childBoxCollider2Ds.Length > 0 || childEdgeCollider2Ds.Length > 0 || childPolygonCollider2Ds.Length > 0))
            {
                EditorUtility.DisplayDialog("StaticSpriteBatcher", "Error: Can't mix 2D and 3D colliders", "Ok");
                return;
            }
#endif

            Dictionary <Transform, int> batchedSpriteLookup = new Dictionary <Transform, int>();
            batchedSpriteLookup[batcher.transform] = -1;

            Matrix4x4 batcherWorldToLocal = batcher.transform.worldToLocalMatrix;

            batcher.spriteCollection = null;
            batcher.batchedSprites   = new tk2dBatchedSprite[allTransforms.Length];
            List <tk2dTextMeshData> allTextMeshData = new List <tk2dTextMeshData>();

            int currBatchedSprite = 0;
            foreach (var t in allTransforms)
            {
                tk2dBaseSprite baseSprite = t.GetComponent <tk2dBaseSprite>();
                tk2dTextMesh   textmesh   = t.GetComponent <tk2dTextMesh>();

                tk2dBatchedSprite bs = new tk2dBatchedSprite();
                bs.name           = t.gameObject.name;
                bs.position       = t.localPosition;
                bs.rotation       = t.localRotation;
                bs.relativeMatrix = batcherWorldToLocal * t.localToWorldMatrix;

                if (baseSprite)
                {
                    bs.baseScale  = Vector3.one;
                    bs.localScale = new Vector3(t.localScale.x * baseSprite.scale.x, t.localScale.y * baseSprite.scale.y, t.localScale.z * baseSprite.scale.z);
                    FillBatchedSprite(bs, t.gameObject);

                    // temp redundant - just incase batcher expects to point to a valid one, somewhere we've missed
                    batcher.spriteCollection = baseSprite.Collection;
                }
                else if (textmesh)
                {
                    bs.spriteCollection = null;

                    bs.type          = tk2dBatchedSprite.Type.TextMesh;
                    bs.color         = textmesh.color;
                    bs.baseScale     = textmesh.scale;
                    bs.renderLayer   = textmesh.SortingOrder;
                    bs.localScale    = new Vector3(t.localScale.x * textmesh.scale.x, t.localScale.y * textmesh.scale.y, t.localScale.z * textmesh.scale.z);
                    bs.FormattedText = textmesh.FormattedText;

                    tk2dTextMeshData tmd = new tk2dTextMeshData();
                    tmd.Font            = textmesh.font;
                    tmd.Text            = textmesh.text;
                    tmd.color           = textmesh.color;
                    tmd.color2          = textmesh.color2;
                    tmd.useGradient     = textmesh.useGradient;
                    tmd.textureGradient = textmesh.textureGradient;
                    tmd.anchor          = textmesh.anchor;
                    tmd.kerning         = textmesh.kerning;
                    tmd.Spacing         = textmesh.Spacing;
                    tmd.LineSpacing     = textmesh.LineSpacing;

                    bs.xRefId = allTextMeshData.Count;
                    allTextMeshData.Add(tmd);
                }
                else
                {
                    // Empty GameObject
                    bs.spriteId   = -1;
                    bs.baseScale  = Vector3.one;
                    bs.localScale = t.localScale;
                    bs.type       = tk2dBatchedSprite.Type.EmptyGameObject;
                }


                batchedSpriteLookup[t] = currBatchedSprite;
                batcher.batchedSprites[currBatchedSprite++] = bs;
            }
            batcher.allTextMeshData = allTextMeshData.ToArray();

            int idx = 0;
            foreach (var t in allTransforms)
            {
                var bs = batcher.batchedSprites[idx];

                bs.parentId = batchedSpriteLookup[t.parent];
                t.parent    = batcher.transform;              // unparent
                ++idx;
            }

            //Transform[] directChildren = (from t in allTransforms where t.parent == batcher.transform select t).ToArray();

            List <Transform> tempdirectChildren = new List <Transform>();
            foreach (Transform t in allTransforms)
            {
                if (t.parent == batcher.transform)
                {
                    tempdirectChildren.Add(t);
                }
            }

            Transform[] directChildren = tempdirectChildren.ToArray();

            foreach (var t in directChildren)
            {
                GameObject.DestroyImmediate(t.gameObject);
            }

            Vector3 inverseScale = new Vector3(1.0f / batcher.scale.x, 1.0f / batcher.scale.y, 1.0f / batcher.scale.z);
            batcher.transform.localScale = Vector3.Scale(batcher.transform.localScale, inverseScale);
            batcher.Build();
            EditorUtility.SetDirty(target);
        }
    }
Esempio n. 11
0
    public void place()
    {
        patternIndex2 = 0;

        GameObject go;
        go = this.gameObject;

        //if (go.transform.childCount > 0)
        //{
        //    Transform[] ch = go.GetComponentsInChildren<Transform>();

        //    foreach (Transform c in ch)
        //    {

        //        DestroyImmediate(c.gameObject);

        //    }

        //}

        spriteCollection = go.transform.parent.GetComponent<tk2dSprite>().Collection;
        selectedSpriteName = go.transform.parent.GetComponent<tk2dSprite>().CurrentSprite.name;

        tk2dStaticSpriteBatcher batcher;

        //if (!this.gameObject.GetComponent<tk2dStaticSpriteBatcher>())
        //{
        //     batcher = this.gameObject.AddComponent<tk2dStaticSpriteBatcher>();
        //}
        //else
        //{

             batcher = this.gameObject.GetComponent<tk2dStaticSpriteBatcher>();
        //}

        batcher.batchedSprites = new tk2dBatchedSprite[points.Length];

        GameObject o = GameObject.CreatePrimitive(PrimitiveType.Quad);

        for (int i = 0; i < batcher.batchedSprites.Length; ++i)
        {

            tk2dBatchedSprite bs = new tk2dBatchedSprite();

            // assign sprite collection and sprite Id for this batched sprite
            bs.spriteCollection = spriteCollection;
            bs.spriteId = spriteCollection.GetSpriteIdByName(selectedSpriteName);

            Vector3 pos = points[i].position;

            o.transform.position = pos;

            o.transform.LookAt(gameObject.transform);

            patternIndex2++;
            Debug.Log("poot");

            if (patternIndex2 == patternbools.Length)
            {
                patternIndex2 = 0;
            }

            if (patternbools[patternIndex2] == false)
            {
                 pos = new Vector3(0f, 0f, 0f);
            }

            // Just lookat
            o.transform.LookAt(gameObject.transform);

            Quaternion q = o.transform.rotation;

            Quaternion qr = Quaternion.Euler(new Vector3(0f, 0f, o.transform.rotation.eulerAngles.z));

           // Debug.Log(points[i].position);

            bs.relativeMatrix.SetTRS(pos, q , Vector3.one * spriteScale);

            //Quaternion.identity

           // bs.position = pos;

            //bs.rotation = qr;

            //bs.localScale = Vector3.one * spriteScale;

            batcher.batchedSprites[i] = bs;

            //bs.rotation = qr;

             // batcher.batchedSprites[i].localScale =  Vector3.one * spriteScale;

            i = i + stepSize;
        }

        // Don't create colliders when you don't need them. It is very expensive to
        // generate colliders at runtime.
        batcher.SetFlag(tk2dStaticSpriteBatcher.Flags.GenerateCollider, false);

        batcher.Build();

        boolupdate = false;
           // return;
    }
	Material GetMaterial(tk2dBatchedSprite bs) {
		switch (bs.type) {
			case tk2dBatchedSprite.Type.EmptyGameObject: return null;
			case tk2dBatchedSprite.Type.TextMesh: return allTextMeshData[bs.xRefId].font.materialInst;
			default: return bs.GetSpriteDefinition().materialInst;
		}
	}
 private Material GetMaterial(tk2dBatchedSprite bs)
 {
     tk2dBatchedSprite.Type type = bs.type;
     if (type != tk2dBatchedSprite.Type.EmptyGameObject)
     {
         if (type == tk2dBatchedSprite.Type.TextMesh)
         {
             return this.allTextMeshData[bs.xRefId].font.materialInst;
         }
         return bs.GetSpriteDefinition().materialInst;
     }
     return null;
 }
    public override void OnInspectorGUI()
    {
        tk2dStaticSpriteBatcher batcher = (tk2dStaticSpriteBatcher)target;

        if (batcher.batchedSprites == null || batcher.batchedSprites.Length == 0)
        {
            if (GUILayout.Button("Commit"))
            {
                List<tk2dSprite> sprites = new List<tk2dSprite>();
                tk2dSpriteCollectionData scd = null;

                for (int i = 0; i < batcher.transform.childCount; ++i)
                {
                    Transform t = batcher.transform.GetChild(i);
                    tk2dSprite s = t.GetComponent<tk2dSprite>();
                    if (s)
                    {
                        if (scd == null) scd = s.collection;
                        if (scd != s.collection)
                        {
                            EditorUtility.DisplayDialog("StaticSpriteBatcher", "Error: Multiple sprite collections found", "Ok");
                            return;
                        }

                        if (scd.allowMultipleAtlases)
                        {
                            EditorUtility.DisplayDialog("StaticSpriteBatcher", "Error: Sprite collections with multiple atlases not allowed", "Ok");
                            return;
                        }

                        sprites.Add(s);
                    }
                }

                // sort sprites, smaller to larger z
                sprites.Sort( (a,b) => b.transform.localPosition.z.CompareTo(a.transform.localPosition.z) );

                batcher.spriteCollection = scd;
                batcher.batchedSprites = new tk2dBatchedSprite[sprites.Count];
                int currBatchedSprite = 0;
                foreach (var s in sprites)
                {
                    tk2dBatchedSprite bs = new tk2dBatchedSprite();

                    bs.name = s.gameObject.name;
                    bs.color = s.color;
                    bs.localScale = new Vector3(s.scale.x * s.transform.localScale.x, s.scale.y * s.transform.localScale.y, s.scale.z * s.transform.localScale.z);
                    bs.position = s.transform.localPosition;
                    bs.rotation = s.transform.localRotation;
                    bs.spriteId = s.spriteId;
                    bs.alwaysPixelPerfect = s.pixelPerfect;

                    batcher.batchedSprites[currBatchedSprite++] = bs;

                    GameObject.DestroyImmediate(s.gameObject);
                }

                batcher.Build();
                EditorUtility.SetDirty(target);
            }
        }
        else
        {
            if (GUILayout.Button("Edit"))
            {
                foreach (var v in batcher.batchedSprites)
                {
                    GameObject go = new GameObject(v.name);
                    go.transform.parent = batcher.transform;
                    go.transform.localPosition = v.position;
                    go.transform.localRotation = v.rotation;

                    tk2dSprite s = go.AddComponent<tk2dSprite>();
                    s.collection = batcher.spriteCollection;
                    s.Build();

                    s.spriteId = v.spriteId;
                    s.EditMode__CreateCollider(); // needed to recreate the collider after setting spriteId

                    s.scale = v.localScale;
                    s.pixelPerfect = v.alwaysPixelPerfect;
                    s.color = v.color;
                }

                batcher.batchedSprites = null;
                batcher.Build();
                EditorUtility.SetDirty(target);
            }
        }
    }
    void DrawEditorGUI()
    {
        if (GUILayout.Button("Commit"))
        {
            List<tk2dSprite> sprites = new List<tk2dSprite>();
            tk2dSpriteCollectionData scd = null;

            for (int i = 0; i < batcher.transform.childCount; ++i)
            {
                Transform t = batcher.transform.GetChild(i);
                tk2dSprite s = t.GetComponent<tk2dSprite>();
                if (s)
                {
                    if (scd == null) scd = s.collection;
                    if (scd != s.collection)
                    {
                        EditorUtility.DisplayDialog("StaticSpriteBatcher", "Error: Multiple sprite collections found", "Ok");
                        return;
                    }

                    sprites.Add(s);
                }
            }

            // sort sprites, smaller to larger z
            sprites.Sort( (a,b) => b.transform.localPosition.z.CompareTo(a.transform.localPosition.z) );

            // and within the z sort by material

            if (sprites.Count == 0)
            {
                EditorUtility.DisplayDialog("StaticSpriteBatcher", "Error: No child sprite objects found", "Ok");
                return;
            }

            batcher.spriteCollection = scd;
            batcher.batchedSprites = new tk2dBatchedSprite[sprites.Count];
            int currBatchedSprite = 0;
            foreach (var s in sprites)
            {
                tk2dBatchedSprite bs = new tk2dBatchedSprite();

                bs.name = s.gameObject.name;
                bs.color = s.color;
                bs.localScale = new Vector3(s.scale.x * s.transform.localScale.x, s.scale.y * s.transform.localScale.y, s.scale.z * s.transform.localScale.z);
                bs.position = s.transform.localPosition;
                bs.rotation = s.transform.localRotation;
                bs.spriteId = s.spriteId;
                bs.alwaysPixelPerfect = s.pixelPerfect;

                batcher.batchedSprites[currBatchedSprite++] = bs;

                GameObject.DestroyImmediate(s.gameObject);
            }

            batcher.scale = batcher.transform.localScale;
            batcher.transform.localScale = Vector3.one;
            batcher.Build();
            EditorUtility.SetDirty(target);
        }
    }
    void DrawEditorGUI()
    {
        if (GUILayout.Button("Commit"))
        {
            List <tk2dSprite>        sprites = new List <tk2dSprite>();
            tk2dSpriteCollectionData scd     = null;

            for (int i = 0; i < batcher.transform.childCount; ++i)
            {
                Transform  t = batcher.transform.GetChild(i);
                tk2dSprite s = t.GetComponent <tk2dSprite>();
                if (s)
                {
                    if (scd == null)
                    {
                        scd = s.collection;
                    }
                    if (scd != s.collection)
                    {
                        EditorUtility.DisplayDialog("StaticSpriteBatcher", "Error: Multiple sprite collections found", "Ok");
                        return;
                    }

                    sprites.Add(s);
                }
            }

            // sort sprites, smaller to larger z
            sprites.Sort((a, b) => b.transform.localPosition.z.CompareTo(a.transform.localPosition.z));

            // and within the z sort by material

            if (sprites.Count == 0)
            {
                EditorUtility.DisplayDialog("StaticSpriteBatcher", "Error: No child sprite objects found", "Ok");
                return;
            }

            batcher.spriteCollection = scd;
            batcher.batchedSprites   = new tk2dBatchedSprite[sprites.Count];
            int currBatchedSprite = 0;
            foreach (var s in sprites)
            {
                tk2dBatchedSprite bs = new tk2dBatchedSprite();

                bs.name               = s.gameObject.name;
                bs.color              = s.color;
                bs.localScale         = new Vector3(s.scale.x * s.transform.localScale.x, s.scale.y * s.transform.localScale.y, s.scale.z * s.transform.localScale.z);
                bs.position           = s.transform.localPosition;
                bs.rotation           = s.transform.localRotation;
                bs.spriteId           = s.spriteId;
                bs.alwaysPixelPerfect = s.pixelPerfect;

                batcher.batchedSprites[currBatchedSprite++] = bs;

                GameObject.DestroyImmediate(s.gameObject);
            }

            batcher.scale = batcher.transform.localScale;
            batcher.transform.localScale = Vector3.one;
            batcher.Build();
            EditorUtility.SetDirty(target);
        }
    }
Esempio n. 17
0
    void DrawEditorGUI()
    {
        if (GUILayout.Button("Commit"))
        {
            // Select all children, EXCLUDING self
            Transform[] allTransforms = batcher.transform.GetComponentsInChildren <Transform>();
            allTransforms = (from t in allTransforms where t != batcher.transform select t).ToArray();

            // sort sprites, smaller to larger z
            if (batcher.CheckFlag(tk2dStaticSpriteBatcher.Flags.SortToCamera))
            {
                tk2dCamera tk2dCam = tk2dCamera.CameraForLayer(batcher.gameObject.layer);
                Camera     cam     = tk2dCam ? tk2dCam.GetComponent <Camera>() : Camera.main;
                allTransforms = (from t in allTransforms orderby cam.WorldToScreenPoint((t.GetComponent <Renderer>() != null) ? t.GetComponent <Renderer>().bounds.center : t.position).z descending select t).ToArray();
            }
            else
            {
                allTransforms = (from t in allTransforms orderby t.GetComponent <Renderer>().bounds.center.z descending select t).ToArray();
            }

            // and within the z sort by material
            if (allTransforms.Length == 0)
            {
                EditorUtility.DisplayDialog("StaticSpriteBatcher", "Error: No child objects found", "Ok");
                return;
            }

            Dictionary <Transform, int> batchedSpriteLookup = new Dictionary <Transform, int>();
            batchedSpriteLookup[batcher.transform] = -1;

            Matrix4x4 batcherWorldToLocal = batcher.transform.worldToLocalMatrix;

            batcher.spriteCollection = null;
            batcher.batchedSprites   = new tk2dBatchedSprite[allTransforms.Length];
            List <tk2dTextMeshData> allTextMeshData = new List <tk2dTextMeshData>();

            int currBatchedSprite = 0;
            foreach (var t in allTransforms)
            {
                tk2dBaseSprite baseSprite = t.GetComponent <tk2dBaseSprite>();
                tk2dTextMesh   textmesh   = t.GetComponent <tk2dTextMesh>();

                tk2dBatchedSprite bs = new tk2dBatchedSprite();
                bs.name           = t.gameObject.name;
                bs.position       = t.localPosition;
                bs.rotation       = t.localRotation;
                bs.relativeMatrix = batcherWorldToLocal * t.localToWorldMatrix;

                if (baseSprite)
                {
                    bs.baseScale  = Vector3.one;
                    bs.localScale = new Vector3(t.localScale.x * baseSprite.scale.x, t.localScale.y * baseSprite.scale.y, t.localScale.z * baseSprite.scale.z);
                    FillBatchedSprite(bs, t.gameObject);

                    // temp redundant - just incase batcher expects to point to a valid one, somewhere we've missed
                    batcher.spriteCollection = baseSprite.Collection;
                }
                else if (textmesh)
                {
                    bs.spriteCollection = null;

                    bs.type          = tk2dBatchedSprite.Type.TextMesh;
                    bs.color         = textmesh.color;
                    bs.baseScale     = textmesh.scale;
                    bs.renderLayer   = textmesh.SortingOrder;
                    bs.localScale    = new Vector3(t.localScale.x * textmesh.scale.x, t.localScale.y * textmesh.scale.y, t.localScale.z * textmesh.scale.z);
                    bs.FormattedText = textmesh.FormattedText;

                    tk2dTextMeshData tmd = new tk2dTextMeshData();
                    tmd.font            = textmesh.font;
                    tmd.text            = textmesh.text;
                    tmd.color           = textmesh.color;
                    tmd.color2          = textmesh.color2;
                    tmd.useGradient     = textmesh.useGradient;
                    tmd.textureGradient = textmesh.textureGradient;
                    tmd.anchor          = textmesh.anchor;
                    tmd.kerning         = textmesh.kerning;
                    tmd.maxChars        = textmesh.maxChars;
                    tmd.inlineStyling   = textmesh.inlineStyling;
                    tmd.formatting      = textmesh.formatting;
                    tmd.wordWrapWidth   = textmesh.wordWrapWidth;
                    tmd.spacing         = textmesh.Spacing;
                    tmd.lineSpacing     = textmesh.LineSpacing;

                    bs.xRefId = allTextMeshData.Count;
                    allTextMeshData.Add(tmd);
                }
                else
                {
                    // Empty GameObject
                    bs.spriteId   = -1;
                    bs.baseScale  = Vector3.one;
                    bs.localScale = t.localScale;
                    bs.type       = tk2dBatchedSprite.Type.EmptyGameObject;
                }


                batchedSpriteLookup[t] = currBatchedSprite;
                batcher.batchedSprites[currBatchedSprite++] = bs;
            }
            batcher.allTextMeshData = allTextMeshData.ToArray();

            int idx = 0;
            foreach (var t in allTransforms)
            {
                var bs = batcher.batchedSprites[idx];

                bs.parentId = batchedSpriteLookup[t.parent];
                t.parent    = batcher.transform;              // unparent
                ++idx;
            }

            Transform[] directChildren = (from t in allTransforms where t.parent == batcher.transform select t).ToArray();
            foreach (var t in directChildren)
            {
                GameObject.DestroyImmediate(t.gameObject);
            }

            Vector3 inverseScale = new Vector3(1.0f / batcher.scale.x, 1.0f / batcher.scale.y, 1.0f / batcher.scale.z);
            batcher.transform.localScale = Vector3.Scale(batcher.transform.localScale, inverseScale);
            batcher.Build();
            EditorUtility.SetDirty(target);
        }
    }
    void DrawEditorGUI()
    {
        if (GUILayout.Button("Commit"))
        {
            // Select all children, EXCLUDING self
            Transform[] allTransforms = batcher.transform.GetComponentsInChildren<Transform>();
            allTransforms = (from t in allTransforms where t != batcher.transform select t).ToArray();

            tk2dSpriteCollectionData scd = null;
            foreach (Transform t in allTransforms)
            {
                tk2dSprite s = t.GetComponent<tk2dSprite>();
                if (s)
                {
                    if (scd == null) scd = s.Collection;
                    if (scd != s.Collection)
                    {
                        EditorUtility.DisplayDialog("StaticSpriteBatcher", "Error: Multiple sprite collections found", "Ok");
                        return;
                    }
                }
            }

            // sort sprites, smaller to larger z
            allTransforms = (from t in allTransforms orderby t.position.z descending select t).ToArray();

            // and within the z sort by material
            if (allTransforms.Length == 0)
            {
                EditorUtility.DisplayDialog("StaticSpriteBatcher", "Error: No child objects found", "Ok");
                return;
            }

            Dictionary<Transform, int> batchedSpriteLookup = new Dictionary<Transform, int>();
            batchedSpriteLookup[batcher.transform] = -1;

            batcher.spriteCollection = scd;
            batcher.batchedSprites = new tk2dBatchedSprite[allTransforms.Length];
            int currBatchedSprite = 0;
            foreach (var t in allTransforms)
            {
                tk2dBatchedSprite bs = new tk2dBatchedSprite();
                bs.name = t.gameObject.name;
                tk2dSprite s = t.GetComponent<tk2dSprite>();
                if (s)
                {
                    bs.color = s.color;
                    bs.localScale = new Vector3(s.scale.x * t.localScale.x, s.scale.y * t.localScale.y, s.scale.z * t.localScale.z);
                    bs.spriteId = s.spriteId;
                    bs.alwaysPixelPerfect = s.pixelPerfect;
                }
                else
                {
                    bs.spriteId = -1;
                    bs.localScale = t.localScale;
                }

                batchedSpriteLookup[t] = currBatchedSprite;
                batcher.batchedSprites[currBatchedSprite++] = bs;
            }

            int idx = 0;
            foreach (var t in allTransforms)
            {
                var bs = batcher.batchedSprites[idx];

                bs.parentId = batchedSpriteLookup[t.parent];
                t.parent = batcher.transform; // unparent

                bs.position = t.localPosition;
                bs.rotation = t.localRotation;

                ++idx;
            }

            Transform[] directChildren = (from t in allTransforms where t.parent == batcher.transform select t).ToArray();
            foreach (var t in directChildren)
            {
                GameObject.DestroyImmediate(t.gameObject);
            }

            batcher.scale = batcher.transform.localScale;
            batcher.transform.localScale = Vector3.one;
            batcher.Build();
            EditorUtility.SetDirty(target);
        }
    }
    public override void OnInspectorGUI()
    {
        tk2dStaticSpriteBatcher batcher = (tk2dStaticSpriteBatcher)target;

        if (batcher.batchedSprites == null || batcher.batchedSprites.Length == 0)
        {
            if (GUILayout.Button("Commit"))
            {
                List <tk2dSprite>        sprites = new List <tk2dSprite>();
                tk2dSpriteCollectionData scd     = null;

                for (int i = 0; i < batcher.transform.childCount; ++i)
                {
                    Transform  t = batcher.transform.GetChild(i);
                    tk2dSprite s = t.GetComponent <tk2dSprite>();
                    if (s)
                    {
                        if (scd == null)
                        {
                            scd = s.collection;
                        }
                        if (scd != s.collection)
                        {
                            EditorUtility.DisplayDialog("StaticSpriteBatcher", "Error: Multiple sprite collections found", "Ok");
                            return;
                        }

                        if (scd.allowMultipleAtlases)
                        {
                            EditorUtility.DisplayDialog("StaticSpriteBatcher", "Error: Sprite collections with multiple atlases not allowed", "Ok");
                            return;
                        }

                        sprites.Add(s);
                    }
                }

                // sort sprites, smaller to larger z
                sprites.Sort((a, b) => b.transform.localPosition.z.CompareTo(a.transform.localPosition.z));

                batcher.spriteCollection = scd;
                batcher.batchedSprites   = new tk2dBatchedSprite[sprites.Count];
                int currBatchedSprite = 0;
                foreach (var s in sprites)
                {
                    tk2dBatchedSprite bs = new tk2dBatchedSprite();

                    bs.name               = s.gameObject.name;
                    bs.color              = s.color;
                    bs.localScale         = s.scale;
                    bs.position           = s.transform.localPosition;
                    bs.rotation           = s.transform.localRotation;
                    bs.spriteId           = s.spriteId;
                    bs.alwaysPixelPerfect = s.pixelPerfect;

                    batcher.batchedSprites[currBatchedSprite++] = bs;

                    GameObject.DestroyImmediate(s.gameObject);
                }

                batcher.Build();
                EditorUtility.SetDirty(target);
            }
        }
        else
        {
            if (GUILayout.Button("Edit"))
            {
                foreach (var v in batcher.batchedSprites)
                {
                    GameObject go = new GameObject(v.name);
                    go.transform.parent        = batcher.transform;
                    go.transform.localPosition = v.position;
                    go.transform.localRotation = v.rotation;

                    tk2dSprite s = go.AddComponent <tk2dSprite>();
                    s.collection = batcher.spriteCollection;
                    s.Build();

                    s.spriteId = v.spriteId;
                    s.EditMode__CreateCollider();                     // needed to recreate the collider after setting spriteId

                    s.scale        = v.localScale;
                    s.pixelPerfect = v.alwaysPixelPerfect;
                    s.color        = v.color;
                }

                batcher.batchedSprites = null;
                batcher.Build();
                EditorUtility.SetDirty(target);
            }
        }
    }
    void DrawEditorGUI()
    {
        if (GUILayout.Button("Commit"))
        {
            // Select all children, EXCLUDING self
            Transform[] allTransforms = batcher.transform.GetComponentsInChildren <Transform>();
            allTransforms = (from t in allTransforms where t != batcher.transform select t).ToArray();

            tk2dSpriteCollectionData scd = null;
            foreach (Transform t in allTransforms)
            {
                tk2dSprite s = t.GetComponent <tk2dSprite>();
                if (s)
                {
                    if (scd == null)
                    {
                        scd = s.Collection;
                    }
                    if (scd != s.Collection)
                    {
                        EditorUtility.DisplayDialog("StaticSpriteBatcher", "Error: Multiple sprite collections found", "Ok");
                        return;
                    }
                }
            }

            // sort sprites, smaller to larger z
            allTransforms = (from t in allTransforms orderby t.position.z descending select t).ToArray();

            // and within the z sort by material
            if (allTransforms.Length == 0)
            {
                EditorUtility.DisplayDialog("StaticSpriteBatcher", "Error: No child objects found", "Ok");
                return;
            }

            Dictionary <Transform, int> batchedSpriteLookup = new Dictionary <Transform, int>();
            batchedSpriteLookup[batcher.transform] = -1;

            batcher.spriteCollection = scd;
            batcher.batchedSprites   = new tk2dBatchedSprite[allTransforms.Length];
            int currBatchedSprite = 0;
            foreach (var t in allTransforms)
            {
                tk2dBatchedSprite bs = new tk2dBatchedSprite();
                bs.name = t.gameObject.name;
                tk2dSprite s = t.GetComponent <tk2dSprite>();
                if (s)
                {
                    bs.color              = s.color;
                    bs.localScale         = new Vector3(s.scale.x * t.localScale.x, s.scale.y * t.localScale.y, s.scale.z * t.localScale.z);
                    bs.spriteId           = s.spriteId;
                    bs.alwaysPixelPerfect = s.pixelPerfect;
                }
                else
                {
                    bs.spriteId   = -1;
                    bs.localScale = t.localScale;
                }

                batchedSpriteLookup[t] = currBatchedSprite;
                batcher.batchedSprites[currBatchedSprite++] = bs;
            }

            int idx = 0;
            foreach (var t in allTransforms)
            {
                var bs = batcher.batchedSprites[idx];

                bs.parentId = batchedSpriteLookup[t.parent];
                t.parent    = batcher.transform;              // unparent

                bs.position = t.localPosition;
                bs.rotation = t.localRotation;

                ++idx;
            }

            Transform[] directChildren = (from t in allTransforms where t.parent == batcher.transform select t).ToArray();
            foreach (var t in directChildren)
            {
                GameObject.DestroyImmediate(t.gameObject);
            }

            batcher.scale = batcher.transform.localScale;
            batcher.transform.localScale = Vector3.one;
            batcher.Build();
            EditorUtility.SetDirty(target);
        }
    }