Search Unity

Grid layout group save order of gameobjects

Discussion in 'UGUI & TextMesh Pro' started by adamers, Sep 19, 2017.

  1. adamers

    adamers

    Joined:
    Apr 2, 2014
    Posts:
    5
    Hi. I want to save in playerprefs order of LayoutElements in Grid layout group. I have 2 scripts to drag and drop UI elements.
    Code (CSharp):
    1. using UnityEngine;
    2. public class DragOrderContainer : MonoBehaviour {
    3.     public GameObject objectBeingDragged { get; set; }
    4.     void Awake () {
    5.         objectBeingDragged = null;
    6.     }
    7. }
    and
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using UnityEngine.EventSystems;
    4. public class DragOrderObject : MonoBehaviour, IPointerEnterHandler, IBeginDragHandler, IDragHandler, IEndDragHandler {
    5.     DragOrderContainer container = null;
    6.     void Start () {
    7.         container = GetComponentInParent<DragOrderContainer>();
    8.     }
    9.     public void OnBeginDrag(PointerEventData eventData)
    10.     {
    11.         container.objectBeingDragged = this.gameObject;
    12.     }
    13.     public void OnDrag(PointerEventData data)
    14.     {
    15.         // Do nothing
    16.         // Apparently this interface needs to exist in order for BeginDrag and EndDrag to work,
    17.         // but we don't actually have anything to do here
    18.     }
    19.     public void OnEndDrag(PointerEventData eventData)
    20.     {
    21.         if (container.objectBeingDragged == this.gameObject) container.objectBeingDragged = null;
    22.     }
    23.     public void OnPointerEnter(PointerEventData eventData)
    24.     {
    25.         GameObject objectBeingDragged = container.objectBeingDragged;
    26.         if (objectBeingDragged != null && objectBeingDragged != this.gameObject)
    27.         {
    28.             objectBeingDragged.transform.SetSiblingIndex(this.transform.GetSiblingIndex());
    29.         }
    30.     }
    31. }
    How can i simple save order somethink like that ?
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.UI;
    6.  
    7. public class Saveposition : MonoBehaviour
    8. {
    9.  
    10.  
    11.     public GameObject[] panel;
    12.  
    13.     void Awake()
    14.     {
    15.         for (int i = 0; i < panel.Length; i++)
    16.         {
    17.  
    18.             panel[i] = PlayerPrefs.transform.GetSiblingIndex(panel[i].name);
    19.         }
    20.  
    21.     }
    22.  
    23.  
    24.     private void OnApplicationQuit()
    25.     {
    26.  
    27.  
    28.         for (int i = 0; i < panel.Length; i++)
    29.         {
    30.             PlayerPrefs.transform.SetSiblingIndex(panel[i].name, panel[i]);
    31.  
    32.         }
    33.     }
    34.  
    35.  
    36.     private void OnApplicationPause(bool pause)
    37.     {
    38.  
    39.  
    40.  
    41.  
    42.         for (int i = 0; i < panel.Length; i++)
    43.         {
    44.             PlayerPrefs.transform.SetSiblingIndex(panel[i].name, panel[i]);
    45.             PlayerPrefs.Save();
    46.  
    47.         }
    48.  
    49.  
    50.     }
    51.  
    52.  
    53. }
    54.  
     
    Last edited: Sep 19, 2017