Skip to content

Instantly share code, notes, and snippets.

@NeatWolf
Forked from noisecrime/AssignSceneName
Created April 27, 2020 10:18
Show Gist options
  • Save NeatWolf/26efff780a45fd00e9098da4a9ae8423 to your computer and use it in GitHub Desktop.
Save NeatWolf/26efff780a45fd00e9098da4a9ae8423 to your computer and use it in GitHub Desktop.
Unity - Adding commands to component context/cog menu.
// NoiseCrime Gist
// 2014.06.21
// Unity Version: 3.5.7+
// This script demonstrates how you can use MenuItem to append commands to a script component context menu.
// The new commands will be available by right-clicking on a component header or clicking the little cog icon.
// Docs: http://docs.unity3d.com/ScriptReference/MenuItem.html
// Note: You must use the current class name/type in both the MenuItem and where the context is used in the code.
using UnityEngine;
using System.Collections;
public class AssignSceneName : MonoBehaviour
{
public string m_SceneName = "Unknown";
#if UNITY_EDITOR
// Adds a command to the context menu to assign the current scene name to class property.
[UnityEditor.MenuItem("CONTEXT/AssignSceneName/Update Scene Name")] // Make sure to use class name here
private static void UpdateSceneName(UnityEditor.MenuCommand command)
{
// Must use the class name/type here to store the current context
AssignSceneName context = (AssignSceneName)command.context;
// Assign Scene Name
context.m_SceneName = ExtractSceneName(UnityEditor.EditorApplication.currentScene);
}
// Reset command
private void Reset()
{
// Assign Scene Name
m_SceneName = ExtractSceneName(UnityEditor.EditorApplication.currentScene);
}
// Use System.IO to quickly extract the actual filename of the scene
private static string ExtractSceneName( string currentScene)
{
return System.IO.Path.GetFileNameWithoutExtension(currentScene);
}
#endif
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment