EgonMilanVotrubec

Scriptable Object With Paging

Mar 1st, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.08 KB | None | 0 0
  1. // ------- MyStringScriptableObject .cs
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. [CreateAssetMenu ( fileName = "MyStringScriptableObject", menuName = "New Scriptable Object/MyStringScriptableObject", order = 1 )]
  6. public class MyStringScriptableObject : ScriptableObject
  7. {
  8.     public List<string> myStrings;
  9.     public MyStringScriptableObject ( )
  10.     {
  11.         myStrings = new List<string> ( );
  12.     }
  13. }
  14.  
  15.  
  16.  
  17. // ------- ListEditor .cs
  18. using System.Collections.Generic;
  19. using UnityEditor;
  20. using UnityEngine;
  21.  
  22. [CustomEditor ( typeof ( MyStringScriptableObject ) )]
  23. public class ListEditor : Editor
  24. {
  25.     int currentIndex = 0;
  26.     int lastIndex = 0;
  27.     int pageSize = 3;
  28.     bool updateList = true;
  29.  
  30.     public override void OnInspectorGUI ( )
  31.     {
  32.         MyStringScriptableObject myStrings = ( MyStringScriptableObject ) target;
  33.         if ( myStrings == null ) return;
  34.  
  35.         // --------------------------------------------------------------------------------- Default Map
  36.         if ( currentIndex == 0 )
  37.         {
  38.             if ( myStrings.myStrings == null ) myStrings.myStrings = new List<string> ( 10 );
  39.             if ( myStrings.myStrings.Count == 0 )
  40.             {
  41.                 lastIndex = 10;
  42.                 for ( int i = 0; i < lastIndex; i++ )
  43.                     myStrings.myStrings.Add ( $"New String at Index [{i}]..." );
  44.             }
  45.             lastIndex = Mathf.Clamp ( currentIndex + pageSize, 0, myStrings.myStrings.Count );
  46.         }
  47.  
  48.         // --------------------------------------------------------------------------------- Previous And Next Buttons
  49.         GUILayout.BeginHorizontal ( );
  50.         if ( GUILayout.Button ( "< Prev", GUILayout.ExpandWidth ( false ) ) )
  51.         {
  52.             if ( currentIndex > 0 )
  53.             {
  54.                 currentIndex = Mathf.Clamp ( currentIndex - pageSize, 0, myStrings.myStrings.Count );
  55.                 lastIndex = Mathf.Clamp ( currentIndex + pageSize, 0, myStrings.myStrings.Count );
  56.             }
  57.             updateList = true;
  58.         }
  59.  
  60.         GUILayout.Space ( 5 );
  61.         if ( GUILayout.Button ( "Next >", GUILayout.ExpandWidth ( false ) ) )
  62.         {
  63.             if ( currentIndex < ( myStrings.myStrings.Count - pageSize ) )
  64.             {
  65.                 currentIndex = Mathf.Clamp ( currentIndex + pageSize, 0, myStrings.myStrings.Count );
  66.                 lastIndex = Mathf.Clamp ( currentIndex + pageSize, 0, myStrings.myStrings.Count );
  67.                 updateList = true;
  68.             }
  69.             updateList = true;
  70.         }
  71.  
  72.         GUILayout.EndHorizontal ( );
  73.         EditorGUIUtility.labelWidth = 200;
  74.         currentIndex = EditorGUILayout.IntField ( "Current Index", currentIndex, GUILayout.ExpandWidth ( false ) );
  75.        
  76.         for ( int i = currentIndex; i < lastIndex; i++ )
  77.         {
  78.             EditorGUI.BeginChangeCheck ( );
  79.             myStrings.myStrings [ i ] = EditorGUILayout.TextField ( $"Text[{i}]", myStrings.myStrings [ i ] );
  80.             if ( EditorGUI.EndChangeCheck ( ) )
  81.                 EditorUtility.SetDirty( target );
  82.         }
  83.  
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment