2D Toolkit Forum
2D Toolkit => Support => Topic started by: wintermute on November 13, 2013, 10:38:02 am
-
Hi,
I recently migrated to 2DToolKit 2.3, upon migration I found the the following parcel of code doesn't work
// Get estimated bounds of a string and add half of it to visible area for additional padding
tk2dTextMesh effectText = EffectPrefab.GetComponent<tk2dTextMesh> ();
Bounds textBounds = effectText.GetEstimatedMeshBoundsForString ("[-1 Fire to Elemental Attack]");
This parcel of code was previously working with 2DToolKit 2.2
I've replaced it with an instantiated game object and it seems to work fine
GameObject sampleEffectTextGo = (GameObject)Instantiate(EffectPrefab);
tk2dTextMesh effectText = sampleEffectTextGo.GetComponent<tk2dTextMesh> ();
Bounds textBounds = effectText.GetEstimatedMeshBoundsForString ("[-1 Fire to Elemental Attack]");
Destroy(sampleEffectTextGo);
One thing I've noticed is that the MeshFilter doesn't have a mesh when it's a prefab, however when instantiated it does have one.
Any ideas as to why this is happening?
-
Forgot to put a more informative description, here's the error:
NullReferenceException: Object reference not set to an instance of an object
tk2dTextMesh.FormatText (System.String& _targetString, System.String _source) (at Assets/TK2DROOT/tk2d/Code/Fonts/tk2dTextMesh.cs:124)
tk2dTextMesh.FormatText (System.String unformattedString) (at Assets/TK2DROOT/tk2d/Code/Fonts/tk2dTextMesh.cs:114)
tk2dTextMesh.GetEstimatedMeshBoundsForString (System.String str) (at Assets/TK2DROOT/tk2d/Code/Fonts/tk2dTextMesh.cs:504)
FoodDetailUIController.UpdateDisplay (UnityEngine.GameObject itemGo) (at Assets/Scripts/UI/ItemMenus/FoodDetailUIController.cs:141)
FoodDetailUIController.set_Item (UnityEngine.GameObject value) (at Assets/Scripts/UI/ItemMenus/FoodDetailUIController.cs:30)
FoodDetailUIController.Update () (at Assets/Scripts/UI/ItemMenus/FoodDetailUIController.cs:61)
-
It looks like it just needs an InitInstance added to FormatText(...) in tk2dTextMesh.cs, like so:
void FormatText(ref string _targetString, string _source)
{
InitInstance();
if (formatting == false || wordWrapWidth == 0 || _fontInst.texelSize == Vector2.zero)
{
_targetString = _source;
return;
}
I'm sure I did this a while back, I might've missed another merge somewhere...
Let me know how that works for you.
-
Still throws an exception
NullReferenceException: Object reference not set to an instance of an object
tk2dTextGeomGen.GetMeshDimensionsForString (System.String str, .GeomData geomData) (at Assets/TK2DROOT/tk2d/Code/Fonts/tk2dTextGeomGen.cs:78)
tk2dTextMesh.GetEstimatedMeshBoundsForString (System.String str) (at Assets/TK2DROOT/tk2d/Code/Fonts/tk2dTextMesh.cs:505)
FoodDetailUIController.UpdateDisplay (UnityEngine.GameObject itemGo) (at Assets/Scripts/UI/ItemMenus/FoodDetailUIController.cs:141)
FoodDetailUIController.set_Item (UnityEngine.GameObject value) (at Assets/Scripts/UI/ItemMenus/FoodDetailUIController.cs:30)
FoodDetailUIController.Update () (at Assets/Scripts/UI/ItemMenus/FoodDetailUIController.cs:61)
-
I'm going to have to dig through history properly, as I am almost 100% certain I fixed all of these recently. You will need an InitInstance() at the top of GetEstimatedMeshBoundsForString() as well.
public Bounds GetEstimatedMeshBoundsForString( string str ) {
InitInstance();
Let me know how that goes, and apologies for the inconvenience, its almost entirely my fault for missing a merge somewhere, or accidentally reverting code after fixing it.
-
I have same problem after update to 2.3:
NullReferenceException: Object reference not set to an instance of an object
tk2dTextGeomGen.GetMeshDimensionsForString (System.String str, .GeomData geomData) (at Assets/TK2DROOT/tk2d/Code/Fonts/tk2dTextGeomGen.cs:39)
tk2dTextGeomGen.SetTextMeshGeom (UnityEngine.Vector3[] pos, UnityEngine.Vector2[] uv, UnityEngine.Vector2[] uv2, UnityEngine.Color32[] color, Int32 offset, .GeomData geomData) (at Assets/TK2DROOT/tk2d/Code/Fonts/tk2dTextGeomGen.cs:298)
tk2dTextMesh.DoNotUse__CommitInternal () (at Assets/TK2DROOT/tk2d/Code/Fonts/tk2dTextMesh.cs:634)
tk2dUpdateManager.FlushQueuesInternal () (at Assets/TK2DROOT/tk2d/Code/Runtime/tk2dUpdateManager.cs:74)
tk2dUpdateManager.LateUpdate () (at Assets/TK2DROOT/tk2d/Code/Runtime/tk2dUpdateManager.cs:57)
To to add InitInstance() - it is not help.
What i need to do?
-
Hi!
any news for GetMeshDimensionsForString error?
In my case, I usually found this error when changing the textmesh's text. It's kinda random error so I can't pinpoint where the exact problem is.
-
No sorry I don't think I have a reliable repro case to this as of yet. If you manage to reproduce this reliably please let me know and I'll get it sorted out.
-
well, somehow the geomData.formattedText was null,
so I kinda have a workaround by encapsulate the whole GetMeshDimensionsForString in conditional:
if(str != null)
{
//2d toolkit content
}
else return Vector2.zero
the strange thing is, the textmesh that fire an error have an initial text, and then I change the text to another string that is not null, so formattedText == null should've never happened, but it did.
EDIT: no luck, the error goes somewhere else, and the text indeed disappear. something must've happened when formatting the text that it set the formatted text to null even though the data is not.
EDIT2: ok, my fault, unity somehow set a class that are supposed to be null to non-null since the class only contain primitive (integer & string)
-
I think this may be due to where you're calling it from. Any ideas of the context and state? Eg. are you calling it from Awake on a component, and, is your text mesh a prefab?
-
yeah, the textmesh is a part of prefab, I change the text in OnEnable function. But as my post edit above, it may be my fault, so you shouldn't think too much of this :)
thanks a lot :D