public void CopyFrom(tk2dSpriteCollectionSize source) { this.type = source.type; this.width = source.width; this.height = source.height; this.orthoSize = source.orthoSize; }
public void Create(tk2dSpriteCollectionSize spriteCollectionSize, Texture texture, tk2dBaseSprite.Anchor anchor) { this.DestroyInternal(); if (texture != null) { this.spriteCollectionSize.CopyFrom(spriteCollectionSize); this.texture = texture; this.anchor = anchor; GameObject parentObject = new GameObject("tk2dSpriteFromTexture - " + texture.name); parentObject.transform.localPosition = Vector3.zero; parentObject.transform.localRotation = Quaternion.identity; parentObject.transform.localScale = Vector3.one; parentObject.hideFlags = HideFlags.DontSave; Vector2 vector = tk2dSpriteGeomGen.GetAnchorOffset(anchor, (float) texture.width, (float) texture.height); string[] names = new string[] { "unnamed" }; Rect[] regions = new Rect[] { new Rect(0f, 0f, (float) texture.width, (float) texture.height) }; Vector2[] anchors = new Vector2[] { vector }; this.spriteCollection = SpriteCollectionGenerator.CreateFromTexture(parentObject, texture, spriteCollectionSize, new Vector2((float) texture.width, (float) texture.height), names, regions, null, anchors, new bool[1]); string str = "SpriteFromTexture " + texture.name; this.spriteCollection.spriteCollectionName = str; this.spriteCollection.spriteDefinitions[0].material.name = str; this.spriteCollection.spriteDefinitions[0].material.hideFlags = HideFlags.DontSave | HideFlags.HideInInspector; this.Sprite.SetSprite(this.spriteCollection, 0); } }
/// <summary> /// Use when you need the sprite to be pixel perfect on a specific tk2dCamera. /// </summary> public static tk2dSpriteCollectionSize ForTk2dCamera(tk2dCamera camera) { tk2dSpriteCollectionSize s = new tk2dSpriteCollectionSize(); tk2dCameraSettings cameraSettings = camera.SettingsRoot.CameraSettings; if (cameraSettings.projection == tk2dCameraSettings.ProjectionType.Orthographic) { switch (cameraSettings.orthographicType) { case tk2dCameraSettings.OrthographicType.PixelsPerMeter: s.type = Type.PixelsPerMeter; s.pixelsPerMeter = cameraSettings.orthographicPixelsPerMeter; break; case tk2dCameraSettings.OrthographicType.OrthographicSize: s.type = Type.Explicit; s.height = camera.nativeResolutionHeight; s.orthoSize = cameraSettings.orthographicSize; break; } } else if (cameraSettings.projection == tk2dCameraSettings.ProjectionType.Perspective) { s.type = Type.PixelsPerMeter; s.pixelsPerMeter = 20; // some random value } return(s); }
public void Create( tk2dSpriteCollectionSize spriteCollectionSize, Texture texture, tk2dBaseSprite.Anchor anchor ) { DestroyInternal(); if (texture != null) { // Copy values this.spriteCollectionSize.CopyFrom( spriteCollectionSize ); this.texture = texture; this.anchor = anchor; Vector2 anchorPos = tk2dSpriteGeomGen.GetAnchorOffset( anchor, texture.width, texture.height ); spriteCollection = tk2dRuntime.SpriteCollectionGenerator.CreateFromTexture( gameObject, texture, spriteCollectionSize, new Vector2(texture.width, texture.height), new string[] { "unnamed" } , new Rect[] { new Rect(0, 0, texture.width, texture.height) }, null, new Vector2[] { anchorPos }, new bool[] { false } ); // don't want to save or see this spriteCollection.hideFlags = HideFlags.HideInInspector; string objName = "SpriteFromTexture " + texture.name; spriteCollection.spriteCollectionName = objName; spriteCollection.spriteDefinitions[0].material.name = objName; spriteCollection.spriteDefinitions[0].material.hideFlags = HideFlags.DontSave | HideFlags.HideInInspector; Sprite.SetSprite( spriteCollection, 0 ); } }
public override void OnInspectorGUI() { tk2dSpriteFromTexture target = (tk2dSpriteFromTexture)this.target; EditorGUIUtility.LookLikeInspector(); EditorGUI.BeginChangeCheck(); Texture texture = EditorGUILayout.ObjectField("Texture", target.texture, typeof(Texture), false) as Texture; tk2dBaseSprite.Anchor anchor = target.anchor; tk2dSpriteCollectionSize spriteCollectionSize = new tk2dSpriteCollectionSize(); spriteCollectionSize.CopyFrom(target.spriteCollectionSize); if (texture != null) { anchor = (tk2dBaseSprite.Anchor)EditorGUILayout.EnumPopup("Anchor", target.anchor); SpriteCollectionSizeField(spriteCollectionSize); } if (EditorGUI.EndChangeCheck()) { Undo.RegisterUndo(target, "Sprite From Texture"); target.Create(spriteCollectionSize, texture, anchor); } }
void DoDemoRuntimeSpriteCollection(tk2dSpriteCollectionSize spriteCollectionSize) { if (GUILayout.Button("Use Full Texture")) { DestroyData(); // Create a sprite, using the entire texture as the sprite Rect region = new Rect(0, 0, runtimeTexture.width, runtimeTexture.height); Vector2 anchor = new Vector2(region.width / 2, region.height / 2); GameObject go = tk2dSprite.CreateFromTexture(runtimeTexture, spriteCollectionSize, region, anchor); spriteInstance = go.GetComponent <tk2dSprite>(); spriteCollectionInstance = spriteInstance.Collection; } if (GUILayout.Button("Extract Region)")) { DestroyData(); // Create a sprite, using a region of the texture as the sprite Rect region = new Rect(79, 243, 215, 200); Vector2 anchor = new Vector2(region.width / 2, region.height / 2); GameObject go = tk2dSprite.CreateFromTexture(runtimeTexture, spriteCollectionSize, region, anchor); spriteInstance = go.GetComponent <tk2dSprite>(); spriteCollectionInstance = spriteInstance.Collection; } if (GUILayout.Button("Extract multiple Sprites")) { DestroyData(); string[] names = new string[] { "Extracted region", "Another region", "Full sprite", }; Rect[] regions = new Rect[] { new Rect(79, 243, 215, 200), new Rect(256, 0, 64, 64), new Rect(0, 0, runtimeTexture.width, runtimeTexture.height) }; Vector2[] anchors = new Vector2[] { new Vector2(regions[0].width / 2, regions[0].height / 2), new Vector2(0, regions[1].height), new Vector2(0, regions[1].height) }; // Create a sprite collection with multiple sprites, using regions of the texture spriteCollectionInstance = tk2dSpriteCollectionData.CreateFromTexture(runtimeTexture, spriteCollectionSize, names, regions, anchors); GameObject go = new GameObject("sprite"); go.transform.localPosition = new Vector3(-1, 0, 0); spriteInstance = go.AddComponent <tk2dSprite>(); spriteInstance.SetSprite(spriteCollectionInstance, 0); go = new GameObject("sprite2"); go.transform.parent = spriteInstance.transform; go.transform.localPosition = new Vector3(2, 0, 0); tk2dSprite sprite = go.AddComponent <tk2dSprite>(); sprite.SetSprite(spriteCollectionInstance, "Another region"); } }
public static void SpriteCollectionSize(tk2dSpriteCollectionSize scs) { GUILayout.BeginHorizontal(); scs.type = (tk2dSpriteCollectionSize.Type)EditorGUILayout.EnumPopup("Size", scs.type); tk2dCamera cam = tk2dCamera.Editor__Inst; GUI.enabled = cam != null; if (GUILayout.Button(new GUIContent("g", "Grab from tk2dCamera"), EditorStyles.miniButton, GUILayout.ExpandWidth(false))) { scs.CopyFrom(tk2dSpriteCollectionSize.ForTk2dCamera(cam)); GUI.changed = true; } GUI.enabled = true; GUILayout.EndHorizontal(); EditorGUI.indentLevel++; switch (scs.type) { case tk2dSpriteCollectionSize.Type.Explicit: scs.orthoSize = EditorGUILayout.FloatField("Ortho Size", scs.orthoSize); scs.height = EditorGUILayout.FloatField("Target Height", scs.height); break; case tk2dSpriteCollectionSize.Type.PixelsPerMeter: scs.pixelsPerMeter = EditorGUILayout.FloatField("Pixels Per Meter", scs.pixelsPerMeter); break; } EditorGUI.indentLevel--; }
void DoDemoTexturePacker(tk2dSpriteCollectionSize spriteCollectionSize) { if (GUILayout.Button("Import")) { DestroyData(); // Create atlas spriteCollectionInstance = tk2dSpriteCollectionData.CreateFromTexturePacker(spriteCollectionSize, texturePackerExportFile.text, texturePackerTexture); GameObject go = new GameObject("sprite"); go.transform.localPosition = new Vector3(-1, 0, 0); spriteInstance = go.AddComponent <tk2dSprite>(); spriteInstance.SetSprite(spriteCollectionInstance, "sun"); go = new GameObject("sprite2"); go.transform.parent = spriteInstance.transform; go.transform.localPosition = new Vector3(2, 0, 0); tk2dSprite sprite = go.AddComponent <tk2dSprite>(); sprite.SetSprite(spriteCollectionInstance, "2dtoolkit_logo"); go = new GameObject("sprite3"); go.transform.parent = spriteInstance.transform; go.transform.localPosition = new Vector3(1, 1, 0); sprite = go.AddComponent <tk2dSprite>(); sprite.SetSprite(spriteCollectionInstance, "button_up"); go = new GameObject("sprite4"); go.transform.parent = spriteInstance.transform; go.transform.localPosition = new Vector3(1, -1, 0); sprite = go.AddComponent <tk2dSprite>(); sprite.SetSprite(spriteCollectionInstance, "Rock"); } }
public void Create( tk2dSpriteCollectionSize spriteCollectionSize, Texture texture, tk2dBaseSprite.Anchor anchor ) { DestroyInternal(); if (texture != null) { // Copy values this.spriteCollectionSize.CopyFrom( spriteCollectionSize ); this.texture = texture; this.anchor = anchor; GameObject go = new GameObject("tk2dSpriteFromTexture - " + texture.name); go.transform.localPosition = Vector3.zero; go.transform.localRotation = Quaternion.identity; go.transform.localScale = Vector3.one; go.hideFlags = HideFlags.DontSave; Vector2 anchorPos = tk2dSpriteGeomGen.GetAnchorOffset( anchor, texture.width, texture.height ); spriteCollection = tk2dRuntime.SpriteCollectionGenerator.CreateFromTexture( go, texture, spriteCollectionSize, new Vector2(texture.width, texture.height), new string[] { "unnamed" } , new Rect[] { new Rect(0, 0, texture.width, texture.height) }, null, new Vector2[] { anchorPos }, new bool[] { false } ); string objName = "SpriteFromTexture " + texture.name; spriteCollection.spriteCollectionName = objName; spriteCollection.spriteDefinitions[0].material.name = objName; spriteCollection.spriteDefinitions[0].material.hideFlags = HideFlags.DontSave | HideFlags.HideInInspector; Sprite.SetSprite( spriteCollection, 0 ); } }
public override void OnInspectorGUI() { tk2dSpriteFromTexture target = (tk2dSpriteFromTexture)this.target; tk2dGuiUtility.LookLikeInspector(); EditorGUI.BeginChangeCheck(); Texture texture = EditorGUILayout.ObjectField("Texture", target.texture, typeof(Texture), false) as Texture; if (texture == null) { tk2dGuiUtility.LookLikeControls(); tk2dGuiUtility.InfoBox("Drag a texture into the texture slot above.", tk2dGuiUtility.WarningLevel.Error); } tk2dBaseSprite.Anchor anchor = target.anchor; tk2dSpriteCollectionSize spriteCollectionSize = new tk2dSpriteCollectionSize(); spriteCollectionSize.CopyFrom(target.spriteCollectionSize); if (texture != null) { anchor = (tk2dBaseSprite.Anchor)EditorGUILayout.EnumPopup("Anchor", target.anchor); tk2dGuiUtility.SpriteCollectionSize(spriteCollectionSize); } if (EditorGUI.EndChangeCheck()) { tk2dUndo.RecordObject(target, "Sprite From Texture"); target.Create(spriteCollectionSize, texture, anchor); } }
private void DoDemoRuntimeSpriteCollection(tk2dSpriteCollectionSize spriteCollectionSize) { if (GUILayout.Button("Use Full Texture", new GUILayoutOption[0])) { this.DestroyData(); Rect region = new Rect(0f, 0f, (float) this.runtimeTexture.width, (float) this.runtimeTexture.height); Vector2 anchor = new Vector2(region.width / 2f, region.height / 2f); this.spriteInstance = tk2dSprite.CreateFromTexture(this.runtimeTexture, spriteCollectionSize, region, anchor, string.Empty).GetComponent<tk2dSprite>(); this.spriteCollectionInstance = this.spriteInstance.Collection; } if (GUILayout.Button("Extract Region)", new GUILayoutOption[0])) { this.DestroyData(); Rect rect2 = new Rect(79f, 243f, 215f, 200f); Vector2 vector2 = new Vector2(rect2.width / 2f, rect2.height / 2f); this.spriteInstance = tk2dSprite.CreateFromTexture(this.runtimeTexture, spriteCollectionSize, rect2, vector2, string.Empty).GetComponent<tk2dSprite>(); this.spriteCollectionInstance = this.spriteInstance.Collection; } if (GUILayout.Button("Extract multiple Sprites", new GUILayoutOption[0])) { this.DestroyData(); string[] names = new string[] { "Extracted region", "Another region", "Full sprite" }; Rect[] regions = new Rect[] { new Rect(79f, 243f, 215f, 200f), new Rect(256f, 0f, 64f, 64f), new Rect(0f, 0f, (float) this.runtimeTexture.width, (float) this.runtimeTexture.height) }; Vector2[] anchors = new Vector2[] { new Vector2(regions[0].width / 2f, regions[0].height / 2f), new Vector2(0f, regions[1].height), new Vector2(0f, regions[1].height) }; this.spriteCollectionInstance = tk2dSpriteCollectionData.CreateFromTexture(this.runtimeTexture, spriteCollectionSize, names, regions, anchors); GameObject obj4 = new GameObject("sprite"); obj4.transform.localPosition = new Vector3(-1f, 0f, 0f); this.spriteInstance = obj4.AddComponent<tk2dSprite>(); this.spriteInstance.SetSprite(this.spriteCollectionInstance, 0); obj4 = new GameObject("sprite2"); obj4.transform.parent = this.spriteInstance.transform; obj4.transform.localPosition = new Vector3(2f, 0f, 0f); obj4.AddComponent<tk2dSprite>().SetSprite(this.spriteCollectionInstance, "Another region"); } }
/// <summary> /// Use when you need the sprite to be pixel perfect on a tk2dCamera. /// </summary> public static tk2dSpriteCollectionSize ForTk2dCamera() { tk2dSpriteCollectionSize s = new tk2dSpriteCollectionSize(); s.type = Type.Tk2dCamera; return(s); }
public static tk2dSpriteCollectionSize ForTk2dCamera(tk2dCamera camera) { tk2dSpriteCollectionSize size = new tk2dSpriteCollectionSize(); tk2dCameraSettings cameraSettings = camera.SettingsRoot.CameraSettings; if (cameraSettings.projection == tk2dCameraSettings.ProjectionType.Orthographic) { tk2dCameraSettings.OrthographicType orthographicType = cameraSettings.orthographicType; if (orthographicType != tk2dCameraSettings.OrthographicType.PixelsPerMeter) { if (orthographicType != tk2dCameraSettings.OrthographicType.OrthographicSize) { return size; } } else { size.type = Type.PixelsPerMeter; size.pixelsPerMeter = cameraSettings.orthographicPixelsPerMeter; return size; } size.type = Type.Explicit; size.height = camera.nativeResolutionHeight; size.orthoSize = cameraSettings.orthographicSize; return size; } if (cameraSettings.projection == tk2dCameraSettings.ProjectionType.Perspective) { size.type = Type.PixelsPerMeter; size.pixelsPerMeter = 100f; } return size; }
public static tk2dSpriteCollectionSize ForTk2dCamera() { tk2dSpriteCollectionSize size = new tk2dSpriteCollectionSize(); size.type = Type.PixelsPerMeter; size.pixelsPerMeter = 1f; return size; }
public void Create(tk2dSpriteCollectionSize spriteCollectionSize, Texture texture, tk2dBaseSprite.Anchor anchor) { DestroyInternal(); if (texture != null) { // Copy values this.spriteCollectionSize.CopyFrom(spriteCollectionSize); this.texture = texture; this.anchor = anchor; Vector2 anchorPos = tk2dSpriteGeomGen.GetAnchorOffset(anchor, texture.width, texture.height); spriteCollection = tk2dRuntime.SpriteCollectionGenerator.CreateFromTexture( gameObject, texture, spriteCollectionSize, new Vector2(texture.width, texture.height), new string[] { "unnamed" }, new Rect[] { new Rect(0, 0, texture.width, texture.height) }, null, new Vector2[] { anchorPos }, new bool[] { false }); // don't want to save or see this spriteCollection.hideFlags = HideFlags.HideInInspector; string objName = "SpriteFromTexture " + texture.name; spriteCollection.spriteCollectionName = objName; spriteCollection.spriteDefinitions[0].material.name = objName; spriteCollection.spriteDefinitions[0].material.hideFlags = HideFlags.DontSave | HideFlags.HideInInspector; Sprite.SetSprite(spriteCollection, 0); } }
public static tk2dSpriteCollectionData CreateFromTexture(Texture texture, tk2dSpriteCollectionSize size, Rect region, Vector2 anchor) { string[] names = new string[] { "Unnamed" }; Rect[] regions = new Rect[] { region }; Vector2[] anchors = new Vector2[] { anchor }; return CreateFromTexture(texture, size, names, regions, anchors); }
public override void OnInspectorGUI() { tk2dSpriteFromTexture target = (tk2dSpriteFromTexture)this.target; EditorGUIUtility.LookLikeInspector(); EditorGUI.BeginChangeCheck(); Texture texture = EditorGUILayout.ObjectField("Texture", target.texture, typeof(Texture), false) as Texture; if (texture == null) { EditorGUIUtility.LookLikeControls(); tk2dGuiUtility.InfoBox("Drag a texture into the texture slot above.", tk2dGuiUtility.WarningLevel.Error); } tk2dBaseSprite.Anchor anchor = target.anchor; tk2dSpriteCollectionSize spriteCollectionSize = new tk2dSpriteCollectionSize(); spriteCollectionSize.CopyFrom( target.spriteCollectionSize ); if (texture != null) { anchor = (tk2dBaseSprite.Anchor)EditorGUILayout.EnumPopup("Anchor", target.anchor); SpriteCollectionSizeField( spriteCollectionSize ); } if (EditorGUI.EndChangeCheck()) { Undo.RegisterUndo( target, "Sprite From Texture" ); target.Create( spriteCollectionSize, texture, anchor ); } }
void setTexture(int index) { int currentTexIndex = 0; if (index >= _tempTextures.Length) { currentTexIndex = 0; } else { if ((index - 1) <= 0) { index = 0; } else { index -= 1; } currentTexIndex = index; if (currentTexIndex <= 7) { //Debug.Log(" !!!!! " + currentTexIndex + " , " + _tempTextures[currentTexIndex].name); tk2dSpriteCollectionSize size = new tk2dSpriteCollectionSize(); size.pixelsPerMeter = 1; _titleTexture.Create(size, _tempTextures[currentTexIndex], tk2dBaseSprite.Anchor.MiddleCenter); _titleTexture.gameObject.name = "Texture : " + name; _titleTexture.GetComponent <tk2dBaseSprite>().color = new Color(_titleTexture.GetComponent <tk2dBaseSprite>().color.r, _titleTexture.GetComponent <tk2dBaseSprite>().color.g, _titleTexture.GetComponent <tk2dBaseSprite>().color.b, 1f); _titleTexture.gameObject.SetActive(true); } } }
void DoDemoTexturePacker(tk2dSpriteCollectionSize spriteCollectionSize) { if (GUILayout.Button("Import")) { DestroyData(); // Create atlas spriteCollectionInstance = tk2dSpriteCollectionData.CreateFromTexturePacker(spriteCollectionSize, texturePackerExportFile.text, texturePackerTexture ); GameObject go = new GameObject("sprite"); go.transform.localPosition = new Vector3(-1, 0, 0); spriteInstance = go.AddComponent<tk2dSprite>(); spriteInstance.SetSprite(spriteCollectionInstance, "sun"); go = new GameObject("sprite2"); go.transform.parent = spriteInstance.transform; go.transform.localPosition = new Vector3(2, 0, 0); tk2dSprite sprite = go.AddComponent<tk2dSprite>(); sprite.SetSprite(spriteCollectionInstance, "2dtoolkit_logo"); go = new GameObject("sprite3"); go.transform.parent = spriteInstance.transform; go.transform.localPosition = new Vector3(1, 1, 0); sprite = go.AddComponent<tk2dSprite>(); sprite.SetSprite(spriteCollectionInstance, "button_up"); go = new GameObject("sprite4"); go.transform.parent = spriteInstance.transform; go.transform.localPosition = new Vector3(1, -1, 0); sprite = go.AddComponent<tk2dSprite>(); sprite.SetSprite(spriteCollectionInstance, "Rock"); } }
public void CopyFrom(tk2dSpriteCollectionSize source) { this.type = source.type; this.width = source.width; this.height = source.height; this.orthoSize = source.orthoSize; this.pixelsPerMeter = source.pixelsPerMeter; }
/// <summary> /// When you are using an ortho camera. Use this to create sprites which will be pixel perfect /// automatically at the resolution and orthoSize given. /// </summary> public static tk2dSpriteCollectionSize PixelsPerMeter(float pixelsPerMeter) { tk2dSpriteCollectionSize s = new tk2dSpriteCollectionSize(); s.type = Type.PixelsPerMeter; s.pixelsPerMeter = pixelsPerMeter; return(s); }
/// <summary> /// When you are using an ortho camera. Use this to create sprites which will be pixel perfect /// automatically at the resolution and orthoSize given. /// </summary> public static tk2dSpriteCollectionSize ForResolution(float orthoSize, float width, float height) { tk2dSpriteCollectionSize s = new tk2dSpriteCollectionSize(); s.type = Type.Explicit; s.orthoSize = orthoSize; s.width = width; s.height = height; return s; }
/// <summary> /// Use when you need the sprite to be pixel perfect on a tk2dCamera. /// </summary> public static tk2dSpriteCollectionSize ForTk2dCamera() { tk2dSpriteCollectionSize s = new tk2dSpriteCollectionSize(); s.type = Type.PixelsPerMeter; s.pixelsPerMeter = 1; return(s); }
void DoDemoRuntimeSpriteCollection(tk2dSpriteCollectionSize spriteCollectionSize) { if (GUILayout.Button("Use Full Texture")) { DestroyData(); // Create a sprite, using the entire texture as the sprite Rect region = new Rect(0, 0, runtimeTexture.width, runtimeTexture.height); Vector2 anchor = new Vector2(region.width / 2, region.height / 2); GameObject go = tk2dSprite.CreateFromTexture(runtimeTexture, spriteCollectionSize, region, anchor); spriteInstance = go.GetComponent<tk2dSprite>(); spriteCollectionInstance = spriteInstance.Collection; } if (GUILayout.Button("Extract Region)")) { DestroyData(); // Create a sprite, using a region of the texture as the sprite Rect region = new Rect(79, 243, 215, 200); Vector2 anchor = new Vector2(region.width / 2, region.height / 2); GameObject go = tk2dSprite.CreateFromTexture(runtimeTexture, spriteCollectionSize, region, anchor); spriteInstance = go.GetComponent<tk2dSprite>(); spriteCollectionInstance = spriteInstance.Collection; } if (GUILayout.Button("Extract multiple Sprites")) { DestroyData(); string[] names = new string[] { "Extracted region", "Another region", "Full sprite", }; Rect[] regions = new Rect[] { new Rect(79, 243, 215, 200), new Rect(256, 0, 64, 64), new Rect(0, 0, runtimeTexture.width, runtimeTexture.height) }; Vector2[] anchors = new Vector2[] { new Vector2(regions[0].width / 2, regions[0].height / 2), new Vector2(0, regions[1].height), new Vector2(0, regions[1].height) }; // Create a sprite collection with multiple sprites, using regions of the texture spriteCollectionInstance = tk2dSpriteCollectionData.CreateFromTexture(runtimeTexture, spriteCollectionSize, names, regions, anchors); GameObject go = new GameObject("sprite"); go.transform.localPosition = new Vector3(-1, 0, 0); spriteInstance = go.AddComponent<tk2dSprite>(); spriteInstance.SetSprite(spriteCollectionInstance, 0); go = new GameObject("sprite2"); go.transform.parent = spriteInstance.transform; go.transform.localPosition = new Vector3(2, 0, 0); tk2dSprite sprite = go.AddComponent<tk2dSprite>(); sprite.SetSprite(spriteCollectionInstance, "Another region"); } }
/// <summary> /// When you are using an ortho camera. Use this to create sprites which will be pixel perfect /// automatically at the resolution and orthoSize given. /// </summary> public static tk2dSpriteCollectionSize ForResolution(float orthoSize, float width, float height) { tk2dSpriteCollectionSize s = new tk2dSpriteCollectionSize(); s.type = Type.Explicit; s.orthoSize = orthoSize; s.width = width; s.height = height; return(s); }
public static tk2dSpriteCollectionData CreateFromTexture( Texture texture, tk2dSpriteCollectionSize size, Vector2 textureDimensions, string[] names, Rect[] regions, Rect[] trimRects, Vector2[] anchors, bool[] rotated) { return CreateFromTexture(null, texture, size, textureDimensions, names, regions, trimRects, anchors, rotated); }
public static tk2dSpriteCollectionData CreateFromTexture( Texture texture, tk2dSpriteCollectionSize size, Vector2 textureDimensions, string[] names, Rect[] regions, Rect[] trimRects, Vector2[] anchors, bool[] rotated) { return(CreateFromTexture(null, texture, size, textureDimensions, names, regions, trimRects, anchors, rotated)); }
/// <summary> /// Create a sprite (and gameObject) displaying the region of the texture specified. /// Use <see cref="tk2dSpriteCollectionData.CreateFromTexture"/> if you need to create a sprite collection /// with multiple sprites. /// </summary> public static GameObject CreateFromTexture <T>(Texture texture, tk2dSpriteCollectionSize size, Rect region, Vector2 anchor) where T : tk2dBaseSprite { tk2dSpriteCollectionData data = tk2dRuntime.SpriteCollectionGenerator.CreateFromTexture(texture, size, region, anchor); if (data == null) { return(null); } GameObject spriteGo = new GameObject(); tk2dBaseSprite.AddComponent <T>(spriteGo, data, 0); return(spriteGo); }
public static bool CreateFromTexture(GameObject spriteGo, Texture texture, Texture maskTexture, tk2dSpriteCollectionSize size, Rect region, Vector2 anchor) { tk2dSpriteCollectionData newCollection = CreateMaskedFromTexture(null, texture, size, region, anchor); if (newCollection == null) { return false; } spriteGo.AddComponent<tk2dMaskedSprite>(); tk2dMaskedSprite component = spriteGo.GetComponent<tk2dMaskedSprite>(); component.maskTexture = maskTexture; component.SetSprite(newCollection, 0); return true; }
void SpriteCollectionSizeField(tk2dSpriteCollectionSize spriteCollectionSize) { GUIContent sc = new GUIContent("Sprite Collection Size", null, "The resolution this sprite will be pixel perfect at"); spriteCollectionSize.type = (tk2dSpriteCollectionSize.Type)EditorGUILayout.EnumPopup(sc, spriteCollectionSize.type); if (spriteCollectionSize.type == tk2dSpriteCollectionSize.Type.Explicit) { EditorGUI.indentLevel++; EditorGUILayout.LabelField("Resolution", ""); EditorGUI.indentLevel++; spriteCollectionSize.width = EditorGUILayout.IntField("Width", (int)spriteCollectionSize.width); spriteCollectionSize.height = EditorGUILayout.IntField("Height", (int)spriteCollectionSize.height); EditorGUI.indentLevel--; spriteCollectionSize.orthoSize = EditorGUILayout.FloatField("Ortho Size", spriteCollectionSize.orthoSize); EditorGUI.indentLevel--; } }
public override void OnInspectorGUI() { tk2dMySpriteFromTexture target = (tk2dMySpriteFromTexture)this.target; #if UNITY_5_4_OR_NEWER EditorGUIUtility.labelWidth = 0; EditorGUIUtility.fieldWidth = 0; #else EditorGUIUtility.LookLikeControls(0, 0); #endif EditorGUI.BeginChangeCheck(); Texture texture = EditorGUILayout.ObjectField("Texture", target.Texture, typeof(Texture), false) as Texture; if (texture == null) { #if UNITY_5_4_OR_NEWER EditorGUIUtility.labelWidth = 0; EditorGUIUtility.fieldWidth = 0; #else EditorGUIUtility.LookLikeControls(); #endif tk2dGuiUtility.InfoBox("Drag a texture into the texture slot above.", tk2dGuiUtility.WarningLevel.Error); } tk2dBaseSprite.Anchor anchor = target.anchor; bool rebuild = target.rebuild; tk2dSpriteCollectionSize spriteCollectionSize = new tk2dSpriteCollectionSize(); spriteCollectionSize.CopyFrom(target.spriteCollectionSize); if (texture != null) { rebuild = EditorGUILayout.Toggle("Rebuild on Awake", target.rebuild); anchor = (tk2dBaseSprite.Anchor)EditorGUILayout.EnumPopup("Anchor", target.anchor); SpriteCollectionSizeField(spriteCollectionSize); } if (EditorGUI.EndChangeCheck()) { Undo.RecordObject(target, "Sprite From Texture"); target.anchor = anchor; target.rebuild = rebuild; target.spriteCollectionSize = spriteCollectionSize; target.Texture = texture; target.ForceBuild(); } if (GUILayout.Button("Force build")) { target.ForceBuild(); } }
void SpriteCollectionSizeField(tk2dSpriteCollectionSize spriteCollectionSize) { GUIContent sc = new GUIContent("Sprite Collection Size", null, "The resolution this sprite will be pixel perfect at"); spriteCollectionSize.type = (tk2dSpriteCollectionSize.Type)EditorGUILayout.EnumPopup(sc, spriteCollectionSize.type); if (spriteCollectionSize.type == tk2dSpriteCollectionSize.Type.Explicit) { EditorGUI.indentLevel++; EditorGUILayout.LabelField("Resolution", ""); EditorGUI.indentLevel++; spriteCollectionSize.width = EditorGUILayout.IntField("Width", (int)spriteCollectionSize.width); spriteCollectionSize.height = EditorGUILayout.IntField("Height", (int)spriteCollectionSize.height); EditorGUI.indentLevel--; spriteCollectionSize.orthoSize = EditorGUILayout.FloatField("Ortho Size", spriteCollectionSize.orthoSize); EditorGUI.indentLevel--; } }
static void DoCreateSpriteObjectFromTexture() { Texture tex = Selection.activeObject as Texture; GameObject go = tk2dEditorUtility.CreateGameObjectInScene("Sprite"); go.AddComponent <tk2dSprite>(); tk2dSpriteFromTexture sft = go.AddComponent <tk2dSpriteFromTexture>(); if (tex != null) { tk2dSpriteCollectionSize scs = tk2dSpriteCollectionSize.Default(); if (tk2dCamera.Instance != null) { scs = tk2dSpriteCollectionSize.ForTk2dCamera(tk2dCamera.Instance); } sft.Create(scs, tex, tk2dBaseSprite.Anchor.MiddleCenter); } Selection.activeGameObject = go; Undo.RegisterCreatedObjectUndo(go, "Create Sprite From Texture"); }
void OnGUI() { tk2dSpriteCollectionSize spriteCollectionSize = tk2dSpriteCollectionSize.Explicit(5, 640); // If using the tk2dCamera, use this: // tk2dSpriteCollectionSize spriteCollectionSize = tk2dSpriteCollectionSize.ForTk2dCamera(); GUILayout.BeginHorizontal(); GUILayout.BeginVertical("box"); GUILayout.Label("Runtime Sprite Collection"); DoDemoRuntimeSpriteCollection(spriteCollectionSize); GUILayout.EndVertical(); GUILayout.BeginVertical("box"); GUILayout.Label("Texture Packer Import"); DoDemoTexturePacker(spriteCollectionSize); GUILayout.EndVertical(); GUILayout.EndHorizontal(); }
public static tk2dSpriteCollectionData CreateFromTexture(GameObject parentObject, Texture texture, tk2dSpriteCollectionSize size, Vector2 textureDimensions, string[] names, Rect[] regions, Rect[] trimRects, Vector2[] anchors, bool[] rotated) { tk2dSpriteCollectionData data = ((parentObject == null) ? new GameObject("SpriteCollection") : parentObject).AddComponent<tk2dSpriteCollectionData>(); data.Transient = true; data.version = 3; data.invOrthoSize = 1f / size.OrthoSize; data.halfTargetHeight = size.TargetHeight * 0.5f; data.premultipliedAlpha = false; string name = "tk2d_custom/BlendVertexColorWithMask"; data.material = new Material(Shader.Find(name)); data.material.mainTexture = texture; data.materials = new Material[] { data.material }; data.textures = new Texture[] { texture }; data.buildKey = UnityEngine.Random.Range(0, 0x7fffffff); float scale = (2f * size.OrthoSize) / size.TargetHeight; Rect trimRect = new Rect(0f, 0f, 0f, 0f); data.spriteDefinitions = new tk2dSpriteDefinition[regions.Length]; for (int i = 0; i < regions.Length; i++) { bool flag = (rotated != null) && rotated[i]; if (trimRects != null) { trimRect = trimRects[i]; } else if (flag) { trimRect.Set(0f, 0f, regions[i].height, regions[i].width); } else { trimRect.Set(0f, 0f, regions[i].width, regions[i].height); } data.spriteDefinitions[i] = CreateDefinitionForRegionInTexture(names[i], textureDimensions, scale, regions[i], trimRect, anchors[i], flag); } foreach (tk2dSpriteDefinition definition in data.spriteDefinitions) { definition.material = data.material; } return data; }
/// <summary> /// Use when you need the sprite to be pixel perfect on a specific tk2dCamera. /// </summary> public static tk2dSpriteCollectionSize ForTk2dCamera( tk2dCamera camera ) { tk2dSpriteCollectionSize s = new tk2dSpriteCollectionSize(); tk2dCameraSettings cameraSettings = camera.SettingsRoot.CameraSettings; if (cameraSettings.projection == tk2dCameraSettings.ProjectionType.Orthographic) { switch (cameraSettings.orthographicType) { case tk2dCameraSettings.OrthographicType.PixelsPerMeter: s.type = Type.PixelsPerMeter; s.pixelsPerMeter = cameraSettings.orthographicPixelsPerMeter; break; case tk2dCameraSettings.OrthographicType.OrthographicSize: s.type = Type.Explicit; s.height = camera.nativeResolutionHeight; s.orthoSize = cameraSettings.orthographicSize; break; } } else if (cameraSettings.projection == tk2dCameraSettings.ProjectionType.Perspective) { s.type = Type.PixelsPerMeter; s.pixelsPerMeter = 100; // some random value } return s; }
public void Create(tk2dSpriteCollectionSize spriteCollectionSize, Texture texture, tk2dBaseSprite.Anchor anchor) { DestroyInternal(); if (texture != null) { // Copy values this.spriteCollectionSize.CopyFrom(spriteCollectionSize); this.texture = texture; this.anchor = anchor; GameObject go = new GameObject("tk2dSpriteFromTexture - " + texture.name); go.transform.localPosition = Vector3.zero; go.transform.localRotation = Quaternion.identity; go.transform.localScale = Vector3.one; go.hideFlags = HideFlags.DontSave; Vector2 anchorPos = tk2dSpriteGeomGen.GetAnchorOffset(anchor, texture.width, texture.height); spriteCollection = tk2dRuntime.SpriteCollectionGenerator.CreateFromTexture( go, texture, spriteCollectionSize, new Vector2(texture.width, texture.height), new string[] { "unnamed" }, new Rect[] { new Rect(0, 0, texture.width, texture.height) }, null, new Vector2[] { anchorPos }, new bool[] { false }); string objName = "SpriteFromTexture " + texture.name; spriteCollection.spriteCollectionName = objName; spriteCollection.spriteDefinitions[0].material.name = objName; spriteCollection.spriteDefinitions[0].material.hideFlags = HideFlags.DontSave | HideFlags.HideInInspector; Sprite.SetSprite(spriteCollection, 0); } }
private void DoDemoTexturePacker(tk2dSpriteCollectionSize spriteCollectionSize) { if (GUILayout.Button("Import", new GUILayoutOption[0])) { this.DestroyData(); this.spriteCollectionInstance = tk2dSpriteCollectionData.CreateFromTexturePacker(spriteCollectionSize, this.texturePackerExportFile.text, this.texturePackerTexture); GameObject obj2 = new GameObject("sprite"); obj2.transform.localPosition = new Vector3(-1f, 0f, 0f); this.spriteInstance = obj2.AddComponent<tk2dSprite>(); this.spriteInstance.SetSprite(this.spriteCollectionInstance, "sun"); obj2 = new GameObject("sprite2"); obj2.transform.parent = this.spriteInstance.transform; obj2.transform.localPosition = new Vector3(2f, 0f, 0f); obj2.AddComponent<tk2dSprite>().SetSprite(this.spriteCollectionInstance, "2dtoolkit_logo"); obj2 = new GameObject("sprite3"); obj2.transform.parent = this.spriteInstance.transform; obj2.transform.localPosition = new Vector3(1f, 1f, 0f); obj2.AddComponent<tk2dSprite>().SetSprite(this.spriteCollectionInstance, "button_up"); obj2 = new GameObject("sprite4"); obj2.transform.parent = this.spriteInstance.transform; obj2.transform.localPosition = new Vector3(1f, -1f, 0f); obj2.AddComponent<tk2dSprite>().SetSprite(this.spriteCollectionInstance, "Rock"); } }
public static tk2dSpriteCollectionData CreateFromTexture(Texture texture, tk2dSpriteCollectionSize size, Rect region, Vector2 anchor) { return(CreateFromTexture(texture, size, new string[] { "Unnamed" }, new Rect[] { region }, new Vector2[] { anchor })); }
// Texture packer import public static tk2dSpriteCollectionData CreateFromTexturePacker( tk2dSpriteCollectionSize spriteCollectionSize, string texturePackerFileContents, Texture texture ) { #if !UNITY_FLASH List<string> names = new List<string>(); List<Rect> rects = new List<Rect>(); List<Rect> trimRects = new List<Rect>(); List<Vector2> anchors = new List<Vector2>(); List<bool> rotated = new List<bool>(); int state = 0; System.IO.TextReader tr = new System.IO.StringReader(texturePackerFileContents); // tmp state bool entryRotated = false; bool entryTrimmed = false; string entryName = ""; Rect entryRect = new Rect(); Rect entryTrimData = new Rect(); Vector2 textureDimensions = Vector2.zero; Vector2 anchor = Vector2.zero; // gonna write a non-allocating parser for this one day. // all these substrings & splits can't be good // but should be a tiny bit better than an xml / json parser... string line = tr.ReadLine(); while (line != null) { if (line.Length > 0) { char cmd = line[0]; switch (state) { case 0: { switch (cmd) { case 'i': break; // ignore version field for now case 'w': textureDimensions.x = Int32.Parse(line.Substring(2)); break; case 'h': textureDimensions.y = Int32.Parse(line.Substring(2)); break; // total number of sprites would be ideal to statically allocate case '~': state++; break; } } break; case 1: { switch (cmd) { case 'n': entryName = line.Substring(2); break; case 'r': entryRotated = Int32.Parse(line.Substring(2)) == 1; break; case 's': { // sprite string[] tokens = line.Split(); entryRect.Set( Int32.Parse(tokens[1]), Int32.Parse(tokens[2]), Int32.Parse(tokens[3]), Int32.Parse(tokens[4]) ); } break; case 'o': { // origin string[] tokens = line.Split(); entryTrimData.Set( Int32.Parse(tokens[1]), Int32.Parse(tokens[2]), Int32.Parse(tokens[3]), Int32.Parse(tokens[4]) ); entryTrimmed = true; } break; case '~': { names.Add(entryName); rotated.Add(entryRotated); rects.Add(entryRect); if (!entryTrimmed) { // The entryRect dimensions will be the wrong way around if the sprite is rotated if (entryRotated) entryTrimData.Set(0, 0, entryRect.height, entryRect.width); else entryTrimData.Set(0, 0, entryRect.width, entryRect.height); } trimRects.Add(entryTrimData); anchor.Set( (int)(entryTrimData.width / 2), (int)(entryTrimData.height / 2) ); anchors.Add( anchor ); entryName = ""; entryTrimmed = false; entryRotated = false; } break; } } break; } } line = tr.ReadLine(); } return CreateFromTexture( texture, spriteCollectionSize, textureDimensions, names.ToArray(), rects.ToArray(), trimRects.ToArray(), anchors.ToArray(), rotated.ToArray() ); #else return null; #endif }
/// <summary> /// Use when you need the sprite to be pixel perfect on a tk2dCamera. /// </summary> public static tk2dSpriteCollectionSize ForTk2dCamera() { tk2dSpriteCollectionSize s = new tk2dSpriteCollectionSize(); s.type = Type.Tk2dCamera; return s; }
public static tk2dSpriteCollectionData CreateFromTexturePacker(tk2dSpriteCollectionSize spriteCollectionSize, string texturePackerFileContents, Texture texture) { List<string> list = new List<string>(); List<Rect> list2 = new List<Rect>(); List<Rect> list3 = new List<Rect>(); List<Vector2> list4 = new List<Vector2>(); List<bool> list5 = new List<bool>(); int num = 0; TextReader reader = new StringReader(texturePackerFileContents); bool item = false; bool flag2 = false; string str = string.Empty; Rect rect = new Rect(); Rect rect2 = new Rect(); Vector2 zero = Vector2.zero; Vector2 vector2 = Vector2.zero; for (string str2 = reader.ReadLine(); str2 != null; str2 = reader.ReadLine()) { char ch; char ch2; if (str2.Length > 0) { ch = str2[0]; switch (num) { case 0: ch2 = ch; switch (ch2) { case 'h': goto Label_00DE; case 'i': { continue; } } if (ch2 == 'w') { zero.x = int.Parse(str2.Substring(2)); } else if (ch2 == '~') { goto Label_00F8; } break; case 1: goto Label_0108; } } continue; Label_00DE: zero.y = int.Parse(str2.Substring(2)); continue; Label_00F8: num++; continue; Label_0108: ch2 = ch; switch (ch2) { case 'n': { str = str2.Substring(2); continue; } case 'o': { string[] strArray2 = str2.Split(new char[0]); rect2.Set((float) int.Parse(strArray2[1]), (float) int.Parse(strArray2[2]), (float) int.Parse(strArray2[3]), (float) int.Parse(strArray2[4])); flag2 = true; continue; } case 'r': { item = int.Parse(str2.Substring(2)) == 1; continue; } case 's': { string[] strArray = str2.Split(new char[0]); rect.Set((float) int.Parse(strArray[1]), (float) int.Parse(strArray[2]), (float) int.Parse(strArray[3]), (float) int.Parse(strArray[4])); continue; } } if (ch2 == '~') { list.Add(str); list5.Add(item); list2.Add(rect); if (!flag2) { if (item) { rect2.Set(0f, 0f, rect.height, rect.width); } else { rect2.Set(0f, 0f, rect.width, rect.height); } } list3.Add(rect2); vector2.Set((float) ((int) (rect2.width / 2f)), (float) ((int) (rect2.height / 2f))); list4.Add(vector2); str = string.Empty; flag2 = false; item = false; } } return CreateFromTexture(texture, spriteCollectionSize, zero, list.ToArray(), list2.ToArray(), list3.ToArray(), list4.ToArray(), list5.ToArray()); }
public static UnityEngine.Object Load(string path, Type type) { if (path == "PlayerCoopCultist" && Player.CoopReplacement != null) { path = Player.CoopReplacement; } else if (path.StartsWithInvariant("Player") && Player.PlayerReplacement != null) { path = Player.PlayerReplacement; } UnityEngine.Object customobj = null; for (int i = 0; i < _Protocols.Length; i++) { var protocol = _Protocols[i]; customobj = protocol.Get(path); if (customobj != null) { return(customobj); } } #if DEBUG if (DumpResources) { Dump.DumpResource(path); } #endif AssetMetadata metadata; bool isJson = false; bool isPatch = false; if (TryGetMapped(path, out metadata, true)) { } else if (TryGetMapped(path + ".json", out metadata)) { isJson = true; } else if (TryGetMapped(path + ".patch.json", out metadata)) { isPatch = true; isJson = true; } if (metadata != null) { if (isJson) { if (isPatch) { UnityEngine.Object obj = Resources.Load(path + ETGModUnityEngineHooks.SkipSuffix); using (JsonHelperReader json = JSONHelper.OpenReadJSON(metadata.Stream)) { json.Read(); // Go to start; return((UnityEngine.Object)json.FillObject(obj)); } } return((UnityEngine.Object)JSONHelper.ReadJSON(metadata.Stream)); } if (t_tk2dSpriteCollectionData == type) { AssetMetadata json = GetMapped(path + ".json"); if (metadata.AssetType == t_Texture2D && json != null) { // Atlas string[] names; Rect[] regions; Vector2[] anchors; AttachPoint[][] attachPoints; AssetSpriteData.ToTK2D(JSONHelper.ReadJSON <List <AssetSpriteData> >(json.Stream), out names, out regions, out anchors, out attachPoints); tk2dSpriteCollectionData sprites = tk2dSpriteCollectionData.CreateFromTexture( Resources.Load <Texture2D>(path), tk2dSpriteCollectionSize.Default(), names, regions, anchors ); for (int i = 0; i < attachPoints.Length; i++) { sprites.SetAttachPoints(i, attachPoints[i]); } return(sprites); } if (metadata.AssetType == t_AssetDirectory) { // Separate textures // TODO create collection from "children" assets tk2dSpriteCollectionData data = new GameObject(path.StartsWithInvariant("sprites/") ? path.Substring(8) : path).AddComponent <tk2dSpriteCollectionData>(); tk2dSpriteCollectionSize size = tk2dSpriteCollectionSize.Default(); data.spriteCollectionName = data.name; data.Transient = true; data.version = 3; data.invOrthoSize = 1f / size.OrthoSize; data.halfTargetHeight = size.TargetHeight * 0.5f; data.premultipliedAlpha = false; data.material = new Material(DefaultSpriteShader); data.materials = new Material[] { data.material }; data.buildKey = UnityEngine.Random.Range(0, int.MaxValue); data.Handle(); data.textures = new Texture2D[data.spriteDefinitions.Length]; for (int i = 0; i < data.spriteDefinitions.Length; i++) { data.textures[i] = data.spriteDefinitions[i].materialInst.mainTexture; } return(data); } } if (t_Texture.IsAssignableFrom(type) || type == t_Texture2D || (type == t_Object && metadata.AssetType == t_Texture2D)) { Texture2D tex = new Texture2D(2, 2); tex.name = path; tex.LoadImage(metadata.Data); tex.filterMode = FilterMode.Point; return(tex); } } UnityEngine.Object orig = Resources.Load(path + ETGModUnityEngineHooks.SkipSuffix, type); if (orig is GameObject) { Objects.HandleGameObject((GameObject)orig); } return(orig); }
/// <summary> /// Create a sprite (and gameObject) displaying the region of the texture specified. /// Use <see cref="tk2dSpriteCollectionData.CreateFromTexture"/> if you need to create a sprite collection /// with multiple sprites. It is your responsibility to destroy the collection when you /// destroy this sprite game object. You can get to it by using sprite.Collection. /// Convenience alias of tk2dBaseSprite.CreateFromTexture<tk2dSprite>(...) /// </summary> public static GameObject CreateFromTexture(Texture texture, tk2dSpriteCollectionSize size, Rect region, Vector2 anchor) { return(tk2dBaseSprite.CreateFromTexture <tk2dSprite>(texture, size, region, anchor)); }
public static tk2dSpriteCollectionData CreateFromTexture(Texture texture, tk2dSpriteCollectionSize size, Rect region, Vector2 anchor) { return CreateFromTexture(texture, size, new string[] { "Unnamed" }, new Rect[] { region }, new Vector2[] { anchor } ); }
/// <summary> /// Create a sprite (and gameObject) displaying the region of the texture specified. /// Use <see cref="tk2dSpriteCollectionData.CreateFromTexture"/> if you need to create a sprite collection /// with multiple sprites. /// Convenience alias of tk2dBaseSprite.CreateFromTexture<tk2dSprite>(...) /// </summary> public static GameObject CreateFromTexture(Texture texture, tk2dSpriteCollectionSize size, Rect region, Vector2 anchor) { return tk2dBaseSprite.CreateFromTexture<tk2dSprite>(texture, size, region, anchor); }
public static void SpriteCollectionSize( tk2dSpriteCollectionSize scs ) { GUILayout.BeginHorizontal(); scs.type = (tk2dSpriteCollectionSize.Type)EditorGUILayout.EnumPopup("Size", scs.type); tk2dCamera cam = tk2dCamera.Editor__Inst; GUI.enabled = cam != null; if (GUILayout.Button(new GUIContent("g", "Grab from tk2dCamera"), EditorStyles.miniButton, GUILayout.ExpandWidth(false))) { scs.CopyFrom( tk2dSpriteCollectionSize.ForTk2dCamera(cam) ); GUI.changed = true; } GUI.enabled = true; GUILayout.EndHorizontal(); EditorGUI.indentLevel++; switch (scs.type) { case tk2dSpriteCollectionSize.Type.Explicit: scs.orthoSize = EditorGUILayout.FloatField("Ortho Size", scs.orthoSize); scs.height = EditorGUILayout.FloatField("Target Height", scs.height); break; case tk2dSpriteCollectionSize.Type.PixelsPerMeter: scs.pixelsPerMeter = EditorGUILayout.FloatField("Pixels Per Meter", scs.pixelsPerMeter); break; } EditorGUI.indentLevel--; }
/// <summary> /// Create a sprite collection at runtime from a texturepacker exported file. /// Ensure this is exported using the "2D Toolkit" export mode in TexturePacker. /// You can find this exporter in Assets/TK2DROOT/tk2d/Goodies/TexturePacker/Exporter /// You can use also use this to load sprite collections at runtime. /// </summary> public static tk2dSpriteCollectionData CreateFromTexturePacker(tk2dSpriteCollectionSize size, string texturePackerData, Texture texture) { return(tk2dRuntime.SpriteCollectionGenerator.CreateFromTexturePacker(size, texturePackerData, texture)); }
/// <summary> /// Create a sprite collection at runtime from a texture and user specified regions. /// Please ensure that names, regions & anchor arrays have same dimension. /// Use <see cref="tk2dBaseSprite.CreateFromTexture"/> if you need to create only one sprite from a texture. /// </summary> public static tk2dSpriteCollectionData CreateFromTexture(Texture texture, tk2dSpriteCollectionSize size, string[] names, Rect[] regions, Vector2[] anchors) { return(tk2dRuntime.SpriteCollectionGenerator.CreateFromTexture(texture, size, names, regions, anchors)); }
// Texture packer import public static tk2dSpriteCollectionData CreateFromTexturePacker(tk2dSpriteCollectionSize spriteCollectionSize, string texturePackerFileContents, Texture texture) { #if !UNITY_FLASH List <string> names = new List <string>(); List <Rect> rects = new List <Rect>(); List <Rect> trimRects = new List <Rect>(); List <Vector2> anchors = new List <Vector2>(); List <bool> rotated = new List <bool>(); int state = 0; System.IO.TextReader tr = new System.IO.StringReader(texturePackerFileContents); // tmp state bool entryRotated = false; bool entryTrimmed = false; string entryName = ""; Rect entryRect = new Rect(); Rect entryTrimData = new Rect(); Vector2 textureDimensions = Vector2.zero; Vector2 anchor = Vector2.zero; // gonna write a non-allocating parser for this one day. // all these substrings & splits can't be good // but should be a tiny bit better than an xml / json parser... string line = tr.ReadLine(); while (line != null) { if (line.Length > 0) { char cmd = line[0]; switch (state) { case 0: { switch (cmd) { case 'i': break; // ignore version field for now case 'w': textureDimensions.x = Int32.Parse(line.Substring(2)); break; case 'h': textureDimensions.y = Int32.Parse(line.Substring(2)); break; // total number of sprites would be ideal to statically allocate case '~': state++; break; } } break; case 1: { switch (cmd) { case 'n': entryName = line.Substring(2); break; case 'r': entryRotated = Int32.Parse(line.Substring(2)) == 1; break; case 's': { // sprite string[] tokens = line.Split(); entryRect.Set(Int32.Parse(tokens[1]), Int32.Parse(tokens[2]), Int32.Parse(tokens[3]), Int32.Parse(tokens[4])); } break; case 'o': { // origin string[] tokens = line.Split(); entryTrimData.Set(Int32.Parse(tokens[1]), Int32.Parse(tokens[2]), Int32.Parse(tokens[3]), Int32.Parse(tokens[4])); entryTrimmed = true; } break; case '~': { names.Add(entryName); rotated.Add(entryRotated); rects.Add(entryRect); if (!entryTrimmed) { // The entryRect dimensions will be the wrong way around if the sprite is rotated if (entryRotated) { entryTrimData.Set(0, 0, entryRect.height, entryRect.width); } else { entryTrimData.Set(0, 0, entryRect.width, entryRect.height); } } trimRects.Add(entryTrimData); anchor.Set((int)(entryTrimData.width / 2), (int)(entryTrimData.height / 2)); anchors.Add(anchor); entryName = ""; entryTrimmed = false; entryRotated = false; } break; } } break; } } line = tr.ReadLine(); } return(CreateFromTexture( texture, spriteCollectionSize, textureDimensions, names.ToArray(), rects.ToArray(), trimRects.ToArray(), anchors.ToArray(), rotated.ToArray())); #else return(null); #endif }
public static tk2dSpriteCollectionData CreateFromTexture(Texture texture, tk2dSpriteCollectionSize size, string[] names, Rect[] regions, Vector2[] anchors) { Vector2 textureDimensions = new Vector2((float) texture.width, (float) texture.height); return CreateFromTexture(texture, size, textureDimensions, names, regions, null, anchors, null); }
public static tk2dSpriteCollectionData CreateFromTexture( GameObject parentObject, Texture texture, tk2dSpriteCollectionSize size, Vector2 textureDimensions, string[] names, Rect[] regions, Rect[] trimRects, Vector2[] anchors, bool[] rotated) { GameObject go = (parentObject != null) ? parentObject : new GameObject("SpriteCollection"); tk2dSpriteCollectionData sc = go.AddComponent <tk2dSpriteCollectionData>(); sc.Transient = true; sc.version = tk2dSpriteCollectionData.CURRENT_VERSION; sc.invOrthoSize = 1.0f / size.OrthoSize; sc.halfTargetHeight = size.TargetHeight * 0.5f; sc.premultipliedAlpha = false; string shaderName = "tk2d/BlendVertexColor"; #if UNITY_EDITOR { Shader ts = Shader.Find(shaderName); string assetPath = UnityEditor.AssetDatabase.GetAssetPath(ts); if (assetPath.ToLower().IndexOf("/resources/") == -1) { UnityEditor.EditorUtility.DisplayDialog("tk2dRuntimeSpriteCollection Error", "The tk2d/BlendVertexColor shader needs to be in a resources folder for this to work.\n\n" + "Create a subdirectory named 'resources' where the shaders are, and move the BlendVertexColor shader into this directory.\n\n" + "eg. TK2DROOT/tk2d/Shaders/Resources/BlendVertexColor\n\n" + "Be sure to do this from within Unity and not from Explorer/Finder.", "Ok"); return(null); } } #endif sc.material = new Material(Shader.Find(shaderName)); sc.material.mainTexture = texture; sc.materials = new Material[1] { sc.material }; sc.textures = new Texture[1] { texture }; sc.buildKey = UnityEngine.Random.Range(0, Int32.MaxValue); float scale = 2.0f * size.OrthoSize / size.TargetHeight; Rect trimRect = new Rect(0, 0, 0, 0); // Generate geometry sc.spriteDefinitions = new tk2dSpriteDefinition[regions.Length]; for (int i = 0; i < regions.Length; ++i) { bool defRotated = (rotated != null) ? rotated[i] : false; if (trimRects != null) { trimRect = trimRects[i]; } else { if (defRotated) { trimRect.Set(0, 0, regions[i].height, regions[i].width); } else { trimRect.Set(0, 0, regions[i].width, regions[i].height); } } sc.spriteDefinitions[i] = CreateDefinitionForRegionInTexture(names[i], textureDimensions, scale, regions[i], trimRect, anchors[i], defRotated); } foreach (var def in sc.spriteDefinitions) { def.material = sc.material; } return(sc); }
public static tk2dSpriteCollectionData CreateMaskedFromTexture(GameObject parentObject, Texture texture, tk2dSpriteCollectionSize size, Rect region, Vector2 anchor) { Vector2 textureDimensions = new Vector2((float) texture.width, (float) texture.height); string[] names = new string[] { "Unnamed" }; Rect[] regions = new Rect[] { region }; Vector2[] anchors = new Vector2[] { anchor }; return CreateFromTexture(parentObject, texture, size, textureDimensions, names, regions, null, anchors, null); }
public static tk2dSpriteCollectionData CreateFromTexture(Texture texture, tk2dSpriteCollectionSize size, string[] names, Rect[] regions, Vector2[] anchors) { Vector2 textureDimensions = new Vector2(texture.width, texture.height); return(CreateFromTexture(texture, size, textureDimensions, names, regions, null, anchors, null)); }
public static GameObject CreateFromTexture(Texture texture, tk2dSpriteCollectionSize size, Rect region, Vector2 anchor, [Optional, DefaultParameterValue("")] string name) { return tk2dBaseSprite.CreateFromTexture<tk2dSprite>(texture, size, region, anchor, name); }
/// <summary> /// Create a sprite collection at runtime from a texture and user specified regions. /// Please ensure that names, regions & anchor arrays have same dimension. /// Use <see cref="tk2dBaseSprite.CreateFromTexture"/> if you need to create only one sprite from a texture. /// </summary> public static tk2dSpriteCollectionData CreateFromTexture(Texture texture, tk2dSpriteCollectionSize size, string[] names, Rect[] regions, Vector2[] anchors) { return tk2dRuntime.SpriteCollectionGenerator.CreateFromTexture(texture, size, names, regions, anchors); }
public static tk2dSpriteCollectionData CreateFromTexture(GameObject parentObject, Texture texture, tk2dSpriteCollectionSize size, Vector2 textureDimensions, string[] names, Rect[] regions, Rect[] trimRects, Vector2[] anchors, bool[] rotated) { tk2dSpriteCollectionData tk2dSpriteCollectionData = parentObject.GetOrAddComponent <tk2dSpriteCollectionData>(); tk2dSpriteCollectionData.material = CreateMaterialFromTextureTk2d(texture); tk2dSpriteCollectionData.materials = new Material[] { tk2dSpriteCollectionData.material }; tk2dSpriteCollectionData.textures = new Texture[] { texture }; float scale = 2f * size.OrthoSize / size.TargetHeight; Rect trimRect = new Rect(0f, 0f, 0f, 0f); tk2dSpriteCollectionData.spriteDefinitions = new tk2dSpriteDefinition[regions.Length]; for (int i = 0; i < regions.Length; i++) { if (trimRects != null) { trimRect = trimRects[i]; } else { trimRect.Set(0f, 0f, regions[i].width, regions[i].height); } tk2dSpriteCollectionData.spriteDefinitions[i] = CreateDefinitionForRegionInTexture(names[i], textureDimensions, scale, regions[i], trimRect, anchors[i], false); } foreach (tk2dSpriteDefinition tk2dSpriteDefinition in tk2dSpriteCollectionData.spriteDefinitions) { tk2dSpriteDefinition.material = tk2dSpriteCollectionData.material; } return(tk2dSpriteCollectionData); }
/// <summary> /// Create a sprite collection at runtime from a texturepacker exported file. /// Ensure this is exported using the "2D Toolkit" export mode in TexturePacker. /// You can find this exporter in Assets/TK2DROOT/tk2d/Goodies/TexturePacker/Exporter /// You can use also use this to load sprite collections at runtime. /// </summary> public static tk2dSpriteCollectionData CreateFromTexturePacker(tk2dSpriteCollectionSize size, string texturePackerData, Texture texture) { return tk2dRuntime.SpriteCollectionGenerator.CreateFromTexturePacker(size, texturePackerData, texture); }
/// <summary> /// When you are using an ortho camera. Use this to create sprites which will be pixel perfect /// automatically at the resolution and orthoSize given. /// </summary> public static tk2dSpriteCollectionSize PixelsPerMeter(float pixelsPerMeter) { tk2dSpriteCollectionSize s = new tk2dSpriteCollectionSize(); s.type = Type.PixelsPerMeter; s.pixelsPerMeter = pixelsPerMeter; return s; }