Skip to content

Instantly share code, notes, and snippets.

@openroomxyz
Created April 21, 2020 14:19
Show Gist options
  • Save openroomxyz/8c4eca5416162d86619544c207d80090 to your computer and use it in GitHub Desktop.
Save openroomxyz/8c4eca5416162d86619544c207d80090 to your computer and use it in GitHub Desktop.
Unity : Can i see an example of [ExecuteInEditMode] and [SelectionBase] ?
[ExecuteInEditMode]
[SelectionBase]
public class CubeEditor : MonoBehaviour
{
[SerializeField][Range(1f, 20f)] float gridSize = 10f;
TextMesh textMesh;
private void Start()
{
textMesh = GetComponentInChildren<TextMesh>();
textMesh.text = "BIP3";
}
void Update()
{
//Debug.Log("Editor causes this update");
Vector3 snapPos;
snapPos.x = Mathf.RoundToInt(transform.position.x / gridSize) * gridSize;
snapPos.z = Mathf.RoundToInt(transform.position.z / gridSize) * gridSize;
transform.position = new Vector3(snapPos.x, 0f, snapPos.z);
string labelText = snapPos.x / gridSize + "," + snapPos.z / gridSize;
textMesh.text = labelText;
gameObject.name = labelText;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment