U3D Custom Inspector item does not trigger Save event solution

Source: Internet
Author: User

1. Description of the problem and solution 1.1. Description

Should only start to do u3d editor plug-in parts of the peer need to understand this article.

The problem stems from the fact that when the UI plugin is being made, it is found that manually modifying the values on the Inspector panel does not trigger a modification event for the U3d editor itself, which prevents the operation from being saved and cannot be undone.

The modification event is triggered by an asterisk ' * ' at the far right of the file name, and the content changes in most text editors are now basically the same.

1.2. Solution

Here I have listed two solutions:

(Below are the obj variables of type Playercontrol for the object to be modified)

 Public class Playercontrol:monobehaviour {    publicint0;      Public list<intnew list<int>();}

1. Take advantage of the Undo class in Unityengine, such as modifying a member variable of type int in obj intval:

 using   Unityeditor;  public  class   Test:editor { override  void   Oninspectorgui () {base  .        Oninspectorgui ();  var  obj = target as   Playercontrol; Undo.recordobject (obj,   obj change          );    Obj.intval  = 1  ; }}

However, if one of the member variables in obj is a reference type and the reference is unchanged at the time of the change, then Recordobject will not take effect. For example, a variable of list type adds an element, the start memory address of this variable is unchanged, but its size increases, this time should use the second method, or re-instantiate the object:

usingSystem.Collections.Generic;usingUnityeditor; Public classTest:editor { Public Override voidOninspectorgui () {Base.        Oninspectorgui (); varobj = target asPlayercontrol; Undo.recordobject (obj,"obj Change"); //OBJ.LISTVAL.ADD (1);//this does not trigger undoObj.listval =Newlist<int>(Obj.listval); OBJ.LISTVAL.ADD (1); }}

2. Use SetDirty in the Editorutility class to mark the object as modified:

using Unityeditor;  Public class Test:editor {    publicoverridevoid  Oninspectorgui ()    {         base. Oninspectorgui ();         var  as Playercontrol;        OBJ.LISTVAL.ADD (1);        Editorutility.setdirty (obj);}    }

Do not know whether the revocation system of U3D itself is a difference in the amount of preservation, it is not good to say that the above two ways respectively.

2. Summary

When you use someone else's stuff (engine, library), look at the description document and interface, especially the interface that helps the related class (the class name contains Helper,tool,utility and so on).

U3D Custom Inspector item does not trigger Save event solution

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.