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 AnC_CN · Sep 19, 2017 at 02:16 AM · unity 5unityeditorserializationreflectionbytearray

Unity custom editor when i modify byteArray field with reflection whitout SerializedProperty, it can not save.

I need some feature in my custom class, like intValue ,stringValue, floatValue and so on. But i don’t define many field for save only one data everytime. So i want to use byte[]. I know it not safe,but i need. I try to test below. This is my Class:

 using UnityEngine;
 using System.Collections;
 using System;
 public class Test : MonoBehaviour {
     [SerializeField]
     private CustomData _data;
 }
 
 [System.Serializable]
 public class CustomData
 {
     public byte[] bytes;
 
     public static byte[] GetByte(int i)
     {
         return BitConverter.GetBytes(i);
     }
     public static int ToInt(byte[] bytes)
     {
         return BitConverter.ToInt32(bytes,0);
     }
     //public ToString()
     //public ToFloat()
     //public ToSomeCustomClass
 }

And this is it editor class:

 using UnityEngine;
 using System.Collections;
 using UnityEditor;
 using System.Reflection;
 
 [CustomEditor(typeof(Test))]
 public class TestEditor : Editor {
 
     private SerializedProperty _data;
     private SerializedProperty _bytes;
 
     private FieldInfo _entryField;
     private void OnEnable()
     {
         _data = serializedObject.FindProperty("_data");
         _bytes = _data.FindPropertyRelative("bytes");
 
         _entryField = typeof(Test).GetField("_data", BindingFlags.NonPublic | BindingFlags.Instance);
     }
     public override void OnInspectorGUI()
     {
         serializedObject.Update();
         //I use one size of array to save one byte
         //---- This is so slow, because it assgin with iteration,if not int but custom data(some big data like more than 200 size of byte array)----
         //int intSize = sizeof(int);
         //if (_bytes.arraySize != intSize)
         //    _bytes.arraySize = intSize;
 
         //byte[] intSrcBytes = new byte[intSize];
         //for (int i = 0; i < intSize; ++i)
         //    intSrcBytes[i] = (byte)_bytes.GetArrayElementAtIndex(i).intValue;
 
         //int result= EditorGUILayout.IntField(CustomData.ToInt(intSrcBytes));
 
         //byte[] intEndBytes = CustomData.GetByte(result);
         //for (int i = 0; i < intSize; ++i)
         //    _bytes.GetArrayElementAtIndex(i).intValue = intEndBytes[i];
         //------------------------------------------------------------------
         //---- This is fast.I get the bytes with reflection. ----
         CustomData data = _entryField.GetValue(target) as CustomData;
         byte[] intFastSrcBytes = data.bytes;
         int resultOfFastSrc = EditorGUILayout.IntField(CustomData.ToInt(intFastSrcBytes));
         data.bytes= CustomData.GetByte(resultOfFastSrc);
         _entryField.SetValue(target, data);
         //--------------------------------------------------------
         serializedObject.ApplyModifiedProperties();
     }
 
 }

At First, i use SerializedProperty to byte[] and byte[] to SerializedProperty at part of annotation.I test it with type of int. Result is fine.But when i use this way to test a custom data of more than 200 size. It so slow.Because it iteration all element from SerializedProperty to byte[] and iteration all element from byte[] to SerializedProperty. Result in slow at Editor on Windows.

And Second. I want to get byte[] directly with reflection. It very good when i get byte[]. But the problem is when i SetValue. I see when i change value with keyboard,and GUI of IntField content in Inspector is changed too. But Editor not display i can go to save this change( not appear "*" any where ). In other word, Editor not think it have been modified. So now i close and restart Unity. My changed content not save. Test show me another, if i do any can save opertion,such as i click the SetActive check box of gameobject at Inspector, it appear "*" ,and i save,then i close an restart Unity,previours changed content will be save.

I need the way of Second.the Problem is when i setValue,but not appear any save tip,i can not to save.I how to solve it,or have other easy solution ?

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Bunny83 · Sep 19, 2017 at 02:57 AM

First of all you shouldn't mix using serializedObject and using target directly. Especially calling "ApplyModifiedProperties" after you did manual direct changes doesn't make sense.

To tell the serialization system to serialize the object you want to use Undo.RecordObject right before you edit it. Note that in your code you actually don't need to use SetValue as the monobehaviour still references the same object. You just change / replace the byte array in that class.

If RecordObject doesn't work, you can try calling "EditorUtility.SetDirty" after the changes. However SetDirty usually doesn't work for scene objects anymore. You can also try using SceneManagement.EditorSceneManager.MarkSceneDirty() and pass in the current loaded scene.

Comment
Add comment · Show 1 · Share
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
avatar image AnC_CN · Sep 20, 2017 at 04:17 AM 0
Share

Thank you very much, Undo.RecordObject is useful

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

143 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity Editor SerializedProperty how to assign to array with larger data directly. 1 Answer

Using UniFileBrowser and LoginPro to upload file = zero bytes uploaded? 0 Answers

Losing serialized data after play button pressed. 0 Answers

Aspect ratio changing on play button hit 0 Answers

How to Install Unity different version 0 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