Hello Guest

Author Topic: Getting tk2dTextMesh Estimated Bounds from a Prefab on 2DToolKit 2.3  (Read 9251 times)

wintermute

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 11
    • View Profile
Hi,

I recently migrated to 2DToolKit 2.3, upon migration I found the the following parcel of code doesn't work

Code: [Select]
// 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
Code: [Select]
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?

wintermute

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Getting tk2dTextMesh Estimated Bounds from a Prefab on 2DToolKit 2.3
« Reply #1 on: November 14, 2013, 03:01:03 am »
Forgot to put a more informative description, here's the error:

Code: [Select]
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)

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Getting tk2dTextMesh Estimated Bounds from a Prefab on 2DToolKit 2.3
« Reply #2 on: November 14, 2013, 02:07:04 pm »
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.

wintermute

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Getting tk2dTextMesh Estimated Bounds from a Prefab on 2DToolKit 2.3
« Reply #3 on: November 15, 2013, 12:55:33 am »
Still throws an exception

Code: [Select]
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)

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Getting tk2dTextMesh Estimated Bounds from a Prefab on 2DToolKit 2.3
« Reply #4 on: November 15, 2013, 10:52:01 am »
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.

Kea

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 40
    • View Profile
Re: Getting tk2dTextMesh Estimated Bounds from a Prefab on 2DToolKit 2.3
« Reply #5 on: December 10, 2013, 12:17:35 pm »
I have same problem after update to 2.3:

Code: [Select]
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?

Sundaerae

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 25
    • View Profile
Re: Getting tk2dTextMesh Estimated Bounds from a Prefab on 2DToolKit 2.3
« Reply #6 on: February 06, 2014, 09:16:53 am »
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.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Getting tk2dTextMesh Estimated Bounds from a Prefab on 2DToolKit 2.3
« Reply #7 on: February 06, 2014, 09:17:42 am »
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.

Sundaerae

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 25
    • View Profile
Re: Getting tk2dTextMesh Estimated Bounds from a Prefab on 2DToolKit 2.3
« Reply #8 on: February 06, 2014, 11:33:44 am »
well, somehow the geomData.formattedText was null, so I kinda have a workaround by encapsulate the whole GetMeshDimensionsForString in conditional:
Code: [Select]
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)
« Last Edit: February 06, 2014, 12:39:10 pm by Sundaerae »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Getting tk2dTextMesh Estimated Bounds from a Prefab on 2DToolKit 2.3
« Reply #9 on: February 06, 2014, 09:50:57 pm »
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?

Sundaerae

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 25
    • View Profile
Re: Getting tk2dTextMesh Estimated Bounds from a Prefab on 2DToolKit 2.3
« Reply #10 on: February 07, 2014, 01:17:23 am »
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