Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by dCalle · Jul 25, 2016 at 11:05 PM · scriptableobjectsaving2d arraycustomeditor

Manually Saving ScriptableObject due to 2D Array...

Hey Guys,

alt text

before you drop the "just look it up" Comments. I show you what I tried

  • SetDirty()

  • AssetDataBase.Refresh() and AssetDatase.SaveAssets()

  • Undo.RecordObject()

  • Make the SO non-static

I freaking don't know anymore.

Everytime I press Play the array doesn't exist and has to be initilaized with the default values. Which means, these 2script are for the dust bin right now. I tried to save it by any means the internet told me to.

So now, you come into play. Save me please. Just look over it and help me figure it out.

The 2 Scripts for Download

FactionsEditor

 using UnityEngine;
 using UnityEditor;
 using System;
 
 [CustomEditor(typeof(Factions))]
 public class FactionsEditor : Editor
 {
     private bool[] folds;
     private int max;
 
     private bool update;
 
     private GUIStyle factionStyle, otherFactionStyle;
 
     public void OnEnable()
     {
         Factions.CheckAndUpdateRelations();
         max = Enum.GetValues(typeof(Faction)).GetLength(0);
         folds = new bool[max];
         factionStyle = new GUIStyle();
         factionStyle.fontStyle = FontStyle.Bold;
         factionStyle.alignment = TextAnchor.MiddleRight;
         otherFactionStyle = new GUIStyle();
         otherFactionStyle.alignment = TextAnchor.MiddleRight;
 
     }
 
 
     public override void OnInspectorGUI()
     {
         // Doesn't work either
         Undo.RecordObject(target, "Factions");
         for (int i = 1; i < max; i++)
         {
             EditorGUILayout.BeginHorizontal();
             folds[i] = EditorGUILayout.Foldout(folds[i], "");
             GUILayout.FlexibleSpace();
             EditorGUILayout.LabelField(Enum.GetName(typeof(Faction), i), factionStyle);
             EditorGUILayout.EndHorizontal();
             if (folds[i])
             {
                 for (int j = 1; j < max; j++)
                 {
                     if (i != j)
                     {
                         EditorGUILayout.BeginHorizontal("Label");
                         EditorGUILayout.LabelField(Enum.GetName(typeof(Faction), j), GUILayout.MaxWidth(120));
                         // Here I used to check for valueChanges, see below
                         int stinker = EditorGUILayout.IntSlider(Factions.GetRelations(i,j), 0, 100, GUILayout.MaxWidth(200));
                         EditorGUILayout.LabelField(GetStand(stinker), otherFactionStyle, GUILayout.MaxWidth(100));
                         Factions.SetRelations(i, j, stinker);
                         EditorGUILayout.EndHorizontal();
                         EditorGUILayout.BeginHorizontal();
                         GUILayout.FlexibleSpace();
                         EditorGUILayout.EndHorizontal();
                     }
                 }
                 EditorGUILayout.BeginHorizontal("Box");
                 EditorGUILayout.LabelField("Towards Neutral", GUILayout.MaxWidth(120));
                 int pimmele = EditorGUILayout.IntSlider(Factions.GetRelations(i, 0), 0, 100, GUILayout.MaxWidth(200));
                 EditorGUILayout.LabelField(GetStand(pimmele), otherFactionStyle, GUILayout.MaxWidth(100));
                 Factions.SetRelations(i, 0, pimmele);
                 EditorGUILayout.EndHorizontal();
 
             }
 
         }
         // Actually created two int[,] called oldValues and values and checked if they changed. it actually was quite slow,
         // but update didn't at least freeze while constantly saving, like before (even though it didn't save shit!)
         if (update)
         {
             EditorUtility.SetDirty(target);
             update = false;
         }
 
     }
 
     private string GetStand(int pimmele)
     {
         if (pimmele <= 10)
         {
             return "War";
         }
         else if (pimmele <= 35)
         {
             return "Cold War";
         }
         else if (pimmele < 65)
         {
             return "Neutral";
         }
         else if (pimmele < 90)
         {
             return "Partnership";
         }
         else
         {
             return "Alliance";
         }
     }
 }

(would really like to ut this stuff in spoilers, but this markdown really is crap)

I used this int[,] because I can't use Lists. The way I constructed this array, was to only use a one way relation, that's why I used Get and Set Relations. To guarantee the consistent one way relation.

Factions

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using System;
 
 [CreateAssetMenu]
 [Serializable]
 public class Factions : ScriptableObject
 {
     public static int[,] relations;
     public static int max;
     public static int GetRelations(int first, int second)
     {
         max = Enum.GetValues(typeof(Faction)).GetLength(0) - 1;
         if (first == second)
         {
             throw new Exception("Factions are the same");
         }
         else if (first > max)
         {
             throw new Exception("First Faction is beyond reach: " + first);
         }
         else if (second > max)
         {
             throw new Exception("Second Faction is beyond reach: " + second);
         }
         else if (second < first)
         {
             return relations[second, first];
         }
         else
             return relations[first, second];
     }
 
     public static void SetRelations(int first, int second, int value)
     {
         max = Enum.GetValues(typeof(Faction)).GetLength(0) - 1;
         if (first == second)
         {
             throw new Exception("Factions are the same");
         }
         else if (first > max)
         {
             throw new Exception("First Faction is beyond reach: " + first);
         }
         else if (second > max)
         {
             throw new Exception("Second Faction is beyond reach: " + second);
         }
         else if (second < first)
         {
             relations[second, first] = value;
         }
         else
             relations[first, second] = value;
     }
 
 
     void OnEnable()
     {
         CheckAndUpdateRelations();
     }
 
     public static void CheckAndUpdateRelations()
     {
         max = Enum.GetValues(typeof(Faction)).GetLength(0) - 1;
         int[,] oldRelations = new int[0, 0];
         int length;
         if (relations == null)
         {
             length = -1;
         }
         else if (relations.GetLength(0) < max)
         {
             oldRelations = relations;
             length = oldRelations.GetLength(0);
         }
         else
             return;
         if (length < max)
         {
             relations = new int[max, max + 1];
         }
         Debug.Log(length);
         for (int i = 0; i < max; i++)
         {
             for (int j = i + 1; j <= max; j++)
             {
                 if (i < length)
                 {
                     relations[i, j] = oldRelations[i,j];
                 }
                 else
                 {
                     relations[i, j] = 50;
                 }
             }
         }
     }
 }
 
 public enum Faction
 {
     None,
     Outlaws,
     Merchants,
     Security
 }
 


scripts.zip (1.9 kB)
capture.png (15.5 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Handling change in Class variables with saving ScriptableObject 2 Answers

Creating new scriptable objects at runtime 1 Answer

Implementing a 2D array as a field on a ScriptableObject? 1 Answer

Scriptable objects become volatile on android 1 Answer

Why is ScriptableObject asset not saving to disk? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges