Mac端使用MonoDevelop实现Unity菜单关卡加载功能的方法求助
Hey there! Let's get your level loading script working on your Mac—this is a common mix of small code tweaks and setup checks, so we'll walk through it step by step.
First: Fix the Code Errors
Looking at your snippet, there are two syntax issues that would break the script no matter which editor you're using:
- Case sensitivity: C# is case-sensitive, so
Publicshould be lowercasepublic. - Typo:
LoadlLevelhas an extra lowercase L—it should beLoadLevel.
If you're using a newer Unity version (5.3+), Application.LoadLevel is also deprecated. Here's the corrected, up-to-date code:
using UnityEngine; // Add this line for newer Unity versions using UnityEngine.SceneManagement; public class MainMenu : MonoBehaviour { public void OnClick(int level) { // For Unity 5.2 and older: // Application.LoadLevel(level); // For Unity 5.3+ (recommended): SceneManager.LoadScene(level); } }
Next: Check MonoDevelop & Unity Setup on Mac
Even with correct code, misconfigured editor links or event bindings can stop things from working. Let's verify these:
Set MonoDevelop as Unity's default script editor
- Open Unity, go to
Unity > Settings > External Tools(on Mac, it's in the top menu bar). - Under "External Script Editor", select
MonoDevelopfrom the dropdown. If it's not listed, click "Browse" and navigate to/Applications/MonoDevelop.app. - Close and reopen Unity to apply the change.
- Open Unity, go to
Verify Script Compilation
- Open your script in MonoDevelop. Look for red underlines—these indicate syntax errors. Fix any you see, then save the file.
- Switch back to Unity and check the Console window (Window > General > Console). If there are compilation errors, Unity won't run your script—fix these first.
Recheck Button Event Binding
- Select your UI Button in the Unity Hierarchy.
- In the Inspector, scroll to the
OnClick()section. - Make sure you've dragged the GameObject with your
MainMenuscript into the object field. - Click the dropdown, select your script (
MainMenu) then theOnClick(int)method. - Enter the correct level number in the integer field (make sure this matches the build index of your scene in
File > Build Settings).
Final Checks
- Ensure your target scene is added to the Build Settings: Go to
File > Build Settings, drag your level scenes from the Project window into the "Scenes In Build" list. The index number here is what you'll pass toLoadScene(). - If MonoDevelop still isn't syncing with Unity, try right-clicking your script in the Project window and selecting
Open With > MonoDevelopto force the connection.
That should get your main menu working! Let me know if you hit any snags along the way.
内容的提问来源于stack exchange,提问作者Yozoraa0ryuu

