2D Toolkit Forum

2D Toolkit => Support => Topic started by: Dakwamine on June 13, 2012, 05:26:44 pm

Title: Is it possible to add tk2dSprite component to an empty GameObject?
Post by: Dakwamine on June 13, 2012, 05:26:44 pm
Hello,

I would like to know if it is possible to add tk2dSprite component to a GameObject, let's say an empty GameObject?

The tk2dSprite adds others linked components Mesh Filter and Mesh Renderer to the empty GameObject, but I have a NullException while I try to display the content of the Tk 2d Sprite component in the inspector:

Code: [Select]
NullReferenceException: Object reference not set to an instance of an object
tk2dSpriteGuiUtility.SpriteCollectionPopup (System.String label, .tk2dSpriteCollectionData currentValue, Boolean allowEdit, Int32 spriteId) (at Assets/TK2DROOT/tk2d/Editor/tk2dSpriteGuiUtility.cs:197)
tk2dSpriteEditor.DrawSpriteEditorGUI (.tk2dBaseSprite sprite) (at Assets/TK2DROOT/tk2d/Editor/Sprites/tk2dSpriteEditor.cs:22)
tk2dSpriteEditor.OnInspectorGUI () (at Assets/TK2DROOT/tk2d/Editor/Sprites/tk2dSpriteEditor.cs:11)
UnityEditor.InspectorWindow.DrawEditors (Boolean isRepaintEvent, UnityEditor.Editor[] editors, Boolean eyeDropperDirty) (at C:/BuildAgent/work/300357e52574df36/Editor/Mono/Inspector/InspectorWindow.cs:888)
UnityEditor.InspectorWindow.OnGUI () (at C:/BuildAgent/work/300357e52574df36/Editor/Mono/Inspector/InspectorWindow.cs:243)
System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture)

Thanks for your help.

Best regards.
Title: Re: Is it possible to add tk2dSprite component to an empty GameObject?
Post by: Dakwamine on June 14, 2012, 03:26:18 pm
To explain further what I am trying to do, I know that we can add a single sprite by using the GameObject>Create Other>tk2d>Sprite menu, but in my case, I would like to replace my old sprite system of existing assets with your system. These existing assets have several scripts and components attached to so I would not like to waste my time to create replicas which where created by the menu then add the same components and settings from the old asset then replace their old prefabs equivalent. So I got an error when I tried this on few of my assets even if I deleted all their components (except the Transform one of course).

But in a broader context, I find it could be very useful to have the possibility to add the sprite component to any asset / game object already created.

What is your position on this?
Title: Re: Is it possible to add tk2dSprite component to an empty GameObject?
Post by: unikronsoftware on June 14, 2012, 05:22:00 pm
You can add a tk2dSprite component to an empty game object, and the easiest way to do that is using the following function.
public static tk2dSprite AddComponent(GameObject go, tk2dSpriteCollectionData spriteCollection, int spriteId)

The sprite collection is necessary, otherwise the code will be littered with branches checking for the validity of sprite collection, and I have really been trying hard to avoid random branches. This helper function will call everything in the right order and should create the component for you.


p.s. There is a bug in the current version, where if spriteId == 0, the sprite mesh won't get created for the first time. You can fix this by changing the function in tk2dBaseSprite to this -

Code: [Select]
public static T AddComponent<T>(GameObject go, tk2dSpriteCollectionData spriteCollection, int spriteId) where T : tk2dBaseSprite
{
T sprite = go.AddComponent<T>();
sprite._spriteId = -1;
sprite.collection = spriteCollection;
sprite.spriteId = spriteId;
return sprite;
}

The missing line in the released version being sprite._spriteId = -1; to initialize it correctly and force a build for the first time.
Title: Re: Is it possible to add tk2dSprite component to an empty GameObject?
Post by: Dakwamine on June 14, 2012, 10:02:27 pm
I didn't noticed that it was a feature included in the 1.75 beta 1 release. I tried the beta 3 and it works excellently. I am moving on this version as this functionality is essential for me. ^^

Thank you very much for your support!

Best regards.
Title: Re: Is it possible to add tk2dSprite component to an empty GameObject?
Post by: alternativee30 on June 16, 2012, 08:51:08 am
Hi i am also trying to achieve this functionality as i already have my player prefabs with other scripts attached already but have been referencing materials and textures individually through unity. My single player prefab is up to 120 draw calls in itself and this is going to be no good for a mobile platform.

I would also like to add a 2dtk sprite to my existing planed character prefab. My prefabs fbx consists of 6 planes, arms, legs, chest and head.

when i go to add the 2dtk script to my plane with my characters chest texture for example...

(http://s18.postimage.org/pzr5j3id5/121.png)

This is probably because it has not been added as a 2dTK component yet like you said.

I am very new to this. How do i go about using the script you provided to  Add my plane as a 2dTK component? Ultimately i would like to create a script to attach to my individual planes that adds it as a 2dTK option to begin with and not have to worry about it again.

I could technically workaround this by completely generating all new planes with GameObject>Create Other>tk2d>Sprite and sizing them accordingly to what mine are sized at and then overwrite my prefab but this seems like a whole lot of wasted time and probably not necessary.
Title: Re: Is it possible to add tk2dSprite component to an empty GameObject?
Post by: unikronsoftware on June 16, 2012, 09:36:34 am
This bug should've been fixed in 1.7 + patch 2. What version are you on?

Note that if you add this to a Unity plane primitive, you should probably manually delete the MeshCollider before adding it.
Title: Re: Is it possible to add tk2dSprite component to an empty GameObject?
Post by: alternativee30 on June 16, 2012, 09:58:28 am
I am updated to 1.70 patch 2 current version on the asset store. I am going to give the beta release a try and see if we cant get it going that way.
Title: Re: Is it possible to add tk2dSprite component to an empty GameObject?
Post by: alternativee30 on June 16, 2012, 10:01:37 am
Heh gonna need that access though=)
Title: Re: Is it possible to add tk2dSprite component to an empty GameObject?
Post by: alternativee30 on June 16, 2012, 12:39:23 pm
Updated to 1.75beta 3 and confirmed that the problem is fixed with this version.
Title: Re: Is it possible to add tk2dSprite component to an empty GameObject?
Post by: unikronsoftware on June 16, 2012, 01:06:46 pm
Glad to hear its sorted.
Title: Re: Is it possible to add tk2dSprite component to an empty GameObject?
Post by: fattie on June 17, 2012, 05:21:56 pm
HEH!  a magic fix.

To be clear this is still a problem in 1.75 beta3, today is Sunday June 17

right ??
Title: Re: Is it possible to add tk2dSprite component to an empty GameObject?
Post by: unikronsoftware on June 17, 2012, 05:56:25 pm
I'll release the next beta shortly. It'll have the other fix in there too. I'm glad these new features are getting tested sooner rather than later, its always annoying to have to fix something 2-3 releases on.