Hi there.
I have searched and searched but can’t find anything that works for me on this… I’m basically trying to manipulate the look of the caret that is displayed when using an InputField UI element that I have created through code. I want it to be less tall and to move down slightly.
I’m creating everything from scratch using code for various reasons, so prefab not an option (although I suspect accessing the caret in a prefab InputField would still require code and I assume suffer from the same issue).
Here is what I’m doing when actually trying to reposition the caret (go, w and h are passed in as parameters)
GameObject theCaret = GameObject.Find(go.name+" Input Caret");
Transform inputCaretTrans = theCaret.GetComponent<RectTransform>();
Vector2 currentPosition = inputCaretTrans.position;
inputCaretTrans.position = new Vector2(currentPosition.x,(currentPosition.y-10.0f));
theCaret.GetComponent<RectTransform>().sizeDelta = new Vector2 (w, h);
Here is the structure that displays in the hierarchy:
Canvas
– Game Object
– – Input Caret
– – Text
The game object has a custom script attached to it that constructs the rest of the object, adds children, assigns components etc.
All elements are in the Unity UI layer.
The code finds the input caret, recognises its current position and knows it has been moved - if I log the new position, it is displayed correctly. But it’s not actually moved on-screen when running the app in Unity. It is where it always was.
The only thing that seems to affect the height or position of the caret is if I change the font size or position of the text component, but then the text is off.
I’ve tried various methods I found on these forums, none of which worked and all seemed to work sporadically for some people and not others based on comments on those threads. I’ve tried running the above in the update method repeatedly in case something was resetting it when I did it just once when initialising the input field.
Completely stuck. I’m new to Unity, so could easily be missing something obvious, but I have 15 years experience with developing games so I’m comfortable with the concepts involved and have applied the same logic to other objects no problems… the caret seems different for some reason.
It seems like it should be incredibly simple to do this. But I’ve been banging my head against a wall for ages now with no progress.
Thanks in advance for any help.
Ian