Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 incorrect · Nov 14, 2014 at 12:08 PM · editortilemonobehavior

In-editor object movement restriction

Hi, community!

I want to make my own tile system and right now I'm working on root object that will act like a controller for tiles, storing general information about tile size etc.

What I want to do is to place this tile root in (0, 0, 0), set it's scale and rotation to (1, 1, 1) and restrict changing it's transform values.

I've tried to use MonoBehavior's OnValidate(), but it does not work properly. I still can drag root and change it's transform's values in editor and it does not resets. Any ideas?

Here is the code, btw.

 using UnityEngine;
 using System.Collections;
 
 public class TS_Root : MonoBehaviour {
 
     private static TS_Root instance;
 
     public static TS_Root Instance
     {
         get
         {
             if(instance == null)
             {
                 GameObject temp = new GameObject();
                 temp.name = "TS_Root";
 
                 instance = temp.AddComponent<TS_Root>();
             }
             return instance;
         }
     }
 
     public static int TileWidth = 64;
     public static int TileHeight = 64;
 
     [ExecuteInEditMode]
     void OnValidate()
     {
         if(instance == null) instance = this;
 
         if(transform.position != Vector3.zero) transform.position = Vector3.zero;
         if(transform.localScale != Vector3.one) transform.position = Vector3.one;
         if(transform.rotation != Quaternion.identity) transform.rotation = Quaternion.identity;
     }
 }
Comment
Add comment · Show 2
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 troien · Nov 14, 2014 at 12:23 PM 0
Share

OnValidate gets called whenever you change something in the inspector of this Component. The Transform is another Component on the same GameObject, and thus you can't get a callback for whenever that component is changed (sadly)

avatar image incorrect · Nov 14, 2014 at 12:43 PM 0
Share

@troien, yeah, I got it. So now I'm looking for a way to solve this problem.

2 Replies

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

Answer by Baste · Nov 14, 2014 at 12:47 PM

A kinda hacky way to fix this is to make your TS_Root execute in edit mode, and then reset it's size on Update:

 [ExecuteInEditMode]
 public class TS_Root : MonoBehaviour {
 
     void Update() {
         transform.position = Vector3.zero;
         transform.localScale = new Vector3(1, 1, 1);
         transform.rotation = Quaternion.identity;
     }
 }

This works pretty well - if you try to move or scale the transform's values, nothing happens.

Comment
Add comment · Show 2 · 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 incorrect · Nov 14, 2014 at 12:56 PM 0
Share

Thnx, also found this way.

avatar image Drigomen · Mar 25, 2017 at 08:11 PM 0
Share

You should really do it on LateUpdate though.

Or else, another component could move it after you've fixed position.

avatar image
1

Answer by GameVortex · Nov 14, 2014 at 12:56 PM

You can use HideFlags for this. Each component on a GameObject has its own hideflag value.

You can use HideFlags.NotEditable to set the component as not editable in the inspctor. If you want to hide the component as well then you can use HideFlags.HideInInspector.

So for restricting movement you can set the Transform to not be editable:

 targetObject.transform.hideFlags = HideFlags.NotEditable;

The Transform will be grayed out and non of its values will be editable, but the handles for the object in the scene will still work, so if you want to restrict that then you need to set the gameOBject to not editable as well:

 targetObject.hideFlags = HideFlags.NotEditable;

This makes layers, tags and Name also not editable though. This also has the unfortunate effect of setting the HideFlags of all attached components to NotEditable as well, so if you want the GameObject to not be editable but specific components on the GameOBject be editable, you will need to specifically enable them afterwards:

 targetObject.GetComponent<MyComponent>().hideFlags = 0;


Comment
Add comment · 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

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

30 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

Related Questions

Missing Monobehavior after switched OS from Windows to Mac 1 Answer

Auto Texture Tiling Script Problem 0 Answers

Access Monobehavior Instance from Static Function of Editor Script 1 Answer

Editor drag&drop override 1 Answer

Setting a custom Tile asset's icon/preview in the Inspector/Project Tab to its sprite 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