When I create C# script (Create -> C# Script) via Unity3d or delete it from Unity3d - Visual Studio shows me the warning window.

enter image description here

it's annoying.

Is there any way to force "ReloadAll" in solution Explorer without the window?

up vote 3 down vote accepted

So, what we have.

Developer from the VS Tools for Unity team said that it is one of their most voted feature requests. And it's surprisingly not trivial to fix, they are working on it.

However, I found some script which can use.. I found it on russian unity community.

Great thanks to DbIMok. Also he posted this script yesterday on forum.unity3d.com

Need to create C# script named as FileModDialogCloser.cs and put it into the Editor folder. Here is the code:

using System;
using System.Runtime.InteropServices;
using UnityEditor;

[InitializeOnLoad]
public class FileModDialogCloser {

        [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
        static extern IntPtr FindWindowByCaption(IntPtr ZeroOnly, string lpWindowName);

        [DllImport("user32.dll")]
        static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

        [DllImport("user32.dll")]
        static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, int dwExtraInfo);

        const string search = "File Modification Detected";
        static IntPtr hwnd;

        static void Check() { 
                hwnd = FindWindowByCaption(IntPtr.Zero, search);
                if ((int)hwnd != 0) {
                        ShowWindow(hwnd, 5);
                        keybd_event(0x0D, 0, 0, 0);
                }
        }

        static FileModDialogCloser() {
                EditorApplication.update += Check;
        }
}

Wondering!

It works for creating and deleting C# scripts via Unity.

As you can see the code has a line keybd_event(0x0D, 0, 0, 0);. This is simulate pressing of Enter button. And that means Reload realization. if you need ReloadAll then you have to do like that:

keybd_event(0x09, 0, 0, 0);
keybd_event(0x0D, 0, 0, 0);

This is pressing of Tab button and then Enter.

SMALL FAULT: When you are creating/deleting script: confirm window opens, runs keybd_event, the window closes. It happen very fast, so you don't need to click any button. But you can see this window on few milliseconds anyway. I think, for now there is no better way to do that.


ADDITIONALLY:

You can create C# script for Unity projects (not just simple C# script) within Visual Studio. Unity will add the file automatically without confirmation window.

The first method (simlple):

If I got it right: when you are using Visual Studio Tools for Unity - that tool are integrating already templates for creating Unity C# scripts into the VS. So, right click on Solution and chose Add -> NewItem. You can see two templates for Unity:

введите сюда описание изображения

введите сюда описание изображения

However, these templates can contains a lot of unnecessary information, comments for example // Use this for initialization, // Update is called once per frame e.t.c. And, as @wondra in comments said: you can add own custom item template for unity scripts.

The second method:

  • Creating file from anywhere.

введите сюда описание изображения

  • Adding whatever you want or delete smth., or example:

введите сюда описание изображения

  • Click File -> ExportTemplate

    введите сюда описание изображения

  • Choose ItemTemlate

введите сюда описание изображения

  • Check your template in the list

введите сюда описание изображения

  • Click Next, Next
  • Set name and description for template, click Finish

введите сюда описание изображения

  • Template will be added into the c:\Users\YOUR_PROFILE\Documents\Visual Studio 2015\My Exported Templates\ folder, also into the c:\Users\YOUR_PROFILE\Documents\Visual Studio 2015\Templates\ItemTemplates\. After VS restart you can see this template in the list just as VS Tools for Unity templates:

введите сюда описание изображения

Here is the video explanation.


Happy End!

You can add the new script directly on Visual Studio on your intended directory in Unity. In that case you will not need to reload. enter image description here

But you will have to do something else. New script in Visual Studio will have some namespaces that used default. You may change or remove them. You will have to add using UnityEngine;at first for Unity related privileges. You will have to add void Start() or void Update() by yourself if you need. New Class should look like: enter image description here

  • 1
    Thanks, I know that. But sometimes I want to create/delete scripts within Unity – Alexey Shimansky Jun 22 '16 at 10:57
  • 1
    You dont have to add using UnityEngine or start()/stop() manually, you can add own custom item template for unity scripts. – wondra Jun 22 '16 at 16:58

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.