Hey guys,
SO i am making an inventory system. Currently making the drag n drop. I made it so when the player drags it it snaps out of the parent slot so it won’t show under the other slots and when the drag n drop ends it snaps back to the original parent. All of this works, however the RectTransform gets really high values when changing the parent. I tried using offsetMin and offsetMax and setting them to 0 but that did not work.
public void OnEndDrag(PointerEventData eventData){
this.transform.SetParent (startParent);
this.transform.position = startParent.position;
this.transform.localPosition = new Vector3(0,0,0);
this.transform.GetComponent<RectTransform> ().offsetMin = new Vector2 (0f, 0f);
this.transform.GetComponent<RectTransform> ().offsetMax = new Vector2 (0f, 0f);
}
It kinda didn’t work. I tried both with true and false. UI element still doesn’t reset the RectTransform values. I just want it to set the left bottom top and right values to 0. The offsetmax and offsetmin are not doing it idk why.
But neither the source slot nor the destination slot does anything to manage the position or size of their children?
A naive solution, I suppose, would be to make each slot have its own layout manager for its children. That’s agonizing, and might have a performance impact.
Another option would be to create a vicar for the object being moved. It hasn’t really been re-parented until the drag operation is complete, so that might work. You could create a little icon object and have that be what is getting dragged. Destroy it when the drag ends regardless of how the drag operation works but only do any re-parenting if there is a successful drop. You could hide the object being dragged or make it translucent to give visual cues.
This is weird. I tried making a delay of 0.01 seconds on reseting the rect transform position and it worked. But i am worried about performance. I mean i don’t know why the hell it doesn’t change the position without a timer.