Skip to main content
deleted 35 characters in body
Source Link
namespace marijnz.EditorCoroutines
{
    public class PlacementObjects : EditorWindow
    {
      ...
    private Worker _worker;
    public static bool pressedbool = false;
    private Thread _thread;
    private UnityEngine.Object _locker;
    public static int _progress;

        void DrawFooter()
        {
            GUILayout.BeginArea(FooterSection);
            GUILayout.BeginVertical();
            if (!pressedbool)
            {
                GUILayout.Label("Выберите json");
                GUILayout.BeginHorizontal();
                stringTextFieldURLjsonfile = GUILayout.TextField(stringTextFieldURLjsonfile);
                if (GUILayout.Button("Обзор...", GUILayout.Width(100)))
                {
                    stringTextFieldURLjsonfile = EditorUtility.OpenFilePanel("Выбрать json", "", "json");
                }
                GUILayout.EndHorizontal();
                if (GUILayout.Button("Старт"))
                {

                    pressedbool = true;
                    _worker = new Worker();
                    _worker.ProcessChanged += worker_ProcessChanged;
                    _worker.WorkCompleted += worker_WorkCompleted;
                    Worker._cancelled = false;
                    _thread = new Thread(_worker.Work);
                    _locker = new UnityEngine.Object();
                    _progress = 0;
                    _thread.Start();

                    // if (count_list_index >= 0)
                    // {
                    //     if (myList[count_list_index].geometry_type == 0)
                    //     {
                    //         ProcessJSONPlaceONmapTextures();
                    //     }
                    //     else if (myList[count_list_index].geometry_type == 1)
                    //     {
                    //         ProcessJSONPlaceONmap();
                    //     }
                    // }

                }
                EditorUtility.ClearProgressBar();
            }
            else
            {
                if (GUILayout.Button("Стоп"))
                {
                    pressedbool = false;
                    Worker.Cancel();
                }
                EditorGUI.ProgressBar(new Rect(3, 45, FooterSection.width - 6, 20), _progress / 100.0f, "progress: " + _progress + "%");
            }
            GUILayout.EndVertical();
            GUILayout.EndArea();
        }
        // IEnumerator LoadingRoutine()
        // {
        //     while (_thread.IsAlive)
        //     {
        //         lock (_locker)
        //         {
        //             _progress = EditorGUILayout.IntSlider(Rect(3, 3, position.width - 6, 15), "progress:", _progress, 0, 100);
        //         }
        //         yield return null;
        //     }
        // }
        public static void worker_WorkCompleted(bool cancelled)
        {
            Action action = () =>
            {
                string messeg = cancelled ? "Процесс отменён" : "Процесс завершён!";
                Debug.Log("messeg=" + messeg);
                pressedbool = false;
            };
            action();
            // if (InvokeRequired)
            //     Invoke(action);
            // else
            //     action();
        }
        public static void worker_ProcessChanged(int progress)
        {
            Action action = () => { _progress = progress; };
            // if (InvokeRequired)
            //     Invoke(action);
            // else
            //     action();
            action();
        }
        void OnInspectorUpdate()
        {
            Repaint();
        }
        ...
    }
}
namespace marijnz.EditorCoroutines
{
    public class PlacementObjects : EditorWindow
    {
      ...
    private Worker _worker;
    public static bool pressedbool = false;
    private Thread _thread;
    private UnityEngine.Object _locker;
    public static int _progress;

        void DrawFooter()
        {
            GUILayout.BeginArea(FooterSection);
            GUILayout.BeginVertical();
            if (!pressedbool)
            {
                GUILayout.Label("Выберите json");
                GUILayout.BeginHorizontal();
                stringTextFieldURLjsonfile = GUILayout.TextField(stringTextFieldURLjsonfile);
                if (GUILayout.Button("Обзор...", GUILayout.Width(100)))
                {
                    stringTextFieldURLjsonfile = EditorUtility.OpenFilePanel("Выбрать json", "", "json");
                }
                GUILayout.EndHorizontal();
                if (GUILayout.Button("Старт"))
                {

                    pressedbool = true;
                    _worker = new Worker();
                    _worker.ProcessChanged += worker_ProcessChanged;
                    _worker.WorkCompleted += worker_WorkCompleted;
                    Worker._cancelled = false;
                    _thread = new Thread(_worker.Work);
                    _locker = new UnityEngine.Object();
                    _progress = 0;
                    _thread.Start();

                    // if (count_list_index >= 0)
                    // {
                    //     if (myList[count_list_index].geometry_type == 0)
                    //     {
                    //         ProcessJSONPlaceONmapTextures();
                    //     }
                    //     else if (myList[count_list_index].geometry_type == 1)
                    //     {
                    //         ProcessJSONPlaceONmap();
                    //     }
                    // }

                }
                EditorUtility.ClearProgressBar();
            }
            else
            {
                if (GUILayout.Button("Стоп"))
                {
                    pressedbool = false;
                    Worker.Cancel();
                }
                EditorGUI.ProgressBar(new Rect(3, 45, FooterSection.width - 6, 20), _progress / 100.0f, "progress: " + _progress + "%");
            }
            GUILayout.EndVertical();
            GUILayout.EndArea();
        }
        // IEnumerator LoadingRoutine()
        // {
        //     while (_thread.IsAlive)
        //     {
        //         lock (_locker)
        //         {
        //             _progress = EditorGUILayout.IntSlider(Rect(3, 3, position.width - 6, 15), "progress:", _progress, 0, 100);
        //         }
        //         yield return null;
        //     }
        // }
        public static void worker_WorkCompleted(bool cancelled)
        {
            Action action = () =>
            {
                string messeg = cancelled ? "Процесс отменён" : "Процесс завершён!";
                Debug.Log("messeg=" + messeg);
                pressedbool = false;
            };
            action();
            // if (InvokeRequired)
            //     Invoke(action);
            // else
            //     action();
        }
        public static void worker_ProcessChanged(int progress)
        {
            Action action = () => { _progress = progress; };
            // if (InvokeRequired)
            //     Invoke(action);
            // else
            //     action();
            action();
        }
        void OnInspectorUpdate()
        {
            Repaint();
        }
        ...
    }
}
namespace marijnz.EditorCoroutines
{
    public class PlacementObjects : EditorWindow
    {
      ...
    private Worker _worker;
    public static bool pressedbool = false;
    private Thread _thread;
    private UnityEngine.Object _locker;
    public static int _progress;

        void DrawFooter()
        {
            GUILayout.BeginArea(FooterSection);
            GUILayout.BeginVertical();
            if (!pressedbool)
            {
                GUILayout.Label("Выберите json");
                GUILayout.BeginHorizontal();
                stringTextFieldURLjsonfile = GUILayout.TextField(stringTextFieldURLjsonfile);
                if (GUILayout.Button("Обзор...", GUILayout.Width(100)))
                {
                    stringTextFieldURLjsonfile = EditorUtility.OpenFilePanel("Выбрать json", "", "json");
                }
                GUILayout.EndHorizontal();
                if (GUILayout.Button("Старт"))
                {

                    pressedbool = true;
                    _worker = new Worker();
                    _worker.ProcessChanged += worker_ProcessChanged;
                    _worker.WorkCompleted += worker_WorkCompleted;
                    Worker._cancelled = false;
                    _thread = new Thread(_worker.Work);
                    _locker = new UnityEngine.Object();
                    _progress = 0;
                    _thread.Start();

                    // if (count_list_index >= 0)
                    // {
                    //     if (myList[count_list_index].geometry_type == 0)
                    //     {
                    //         ProcessJSONPlaceONmapTextures();
                    //     }
                    //     else if (myList[count_list_index].geometry_type == 1)
                    //     {
                    //         ProcessJSONPlaceONmap();
                    //     }
                    // }

                }
              
            }
            else
            {
                if (GUILayout.Button("Стоп"))
                {
                    pressedbool = false;
                    Worker.Cancel();
                }
                EditorGUI.ProgressBar(new Rect(3, 45, FooterSection.width - 6, 20), _progress / 100.0f, "progress: " + _progress + "%");
            }
            GUILayout.EndVertical();
            GUILayout.EndArea();
        }
        // IEnumerator LoadingRoutine()
        // {
        //     while (_thread.IsAlive)
        //     {
        //         lock (_locker)
        //         {
        //             _progress = EditorGUILayout.IntSlider(Rect(3, 3, position.width - 6, 15), "progress:", _progress, 0, 100);
        //         }
        //         yield return null;
        //     }
        // }
        public static void worker_WorkCompleted(bool cancelled)
        {
            Action action = () =>
            {
                string messeg = cancelled ? "Процесс отменён" : "Процесс завершён!";
                Debug.Log("messeg=" + messeg);
                pressedbool = false;
            };
            action();
            // if (InvokeRequired)
            //     Invoke(action);
            // else
            //     action();
        }
        public static void worker_ProcessChanged(int progress)
        {
            Action action = () => { _progress = progress; };
            // if (InvokeRequired)
            //     Invoke(action);
            // else
            //     action();
            action();
        }
        void OnInspectorUpdate()
        {
            Repaint();
        }
        ...
    }
}
Source Link

I got this code:

PlacementObjects

namespace marijnz.EditorCoroutines
{
    public class PlacementObjects : EditorWindow
    {
      ...
    private Worker _worker;
    public static bool pressedbool = false;
    private Thread _thread;
    private UnityEngine.Object _locker;
    public static int _progress;

        void DrawFooter()
        {
            GUILayout.BeginArea(FooterSection);
            GUILayout.BeginVertical();
            if (!pressedbool)
            {
                GUILayout.Label("Выберите json");
                GUILayout.BeginHorizontal();
                stringTextFieldURLjsonfile = GUILayout.TextField(stringTextFieldURLjsonfile);
                if (GUILayout.Button("Обзор...", GUILayout.Width(100)))
                {
                    stringTextFieldURLjsonfile = EditorUtility.OpenFilePanel("Выбрать json", "", "json");
                }
                GUILayout.EndHorizontal();
                if (GUILayout.Button("Старт"))
                {

                    pressedbool = true;
                    _worker = new Worker();
                    _worker.ProcessChanged += worker_ProcessChanged;
                    _worker.WorkCompleted += worker_WorkCompleted;
                    Worker._cancelled = false;
                    _thread = new Thread(_worker.Work);
                    _locker = new UnityEngine.Object();
                    _progress = 0;
                    _thread.Start();

                    // if (count_list_index >= 0)
                    // {
                    //     if (myList[count_list_index].geometry_type == 0)
                    //     {
                    //         ProcessJSONPlaceONmapTextures();
                    //     }
                    //     else if (myList[count_list_index].geometry_type == 1)
                    //     {
                    //         ProcessJSONPlaceONmap();
                    //     }
                    // }

                }
                EditorUtility.ClearProgressBar();
            }
            else
            {
                if (GUILayout.Button("Стоп"))
                {
                    pressedbool = false;
                    Worker.Cancel();
                }
                EditorGUI.ProgressBar(new Rect(3, 45, FooterSection.width - 6, 20), _progress / 100.0f, "progress: " + _progress + "%");
            }
            GUILayout.EndVertical();
            GUILayout.EndArea();
        }
        // IEnumerator LoadingRoutine()
        // {
        //     while (_thread.IsAlive)
        //     {
        //         lock (_locker)
        //         {
        //             _progress = EditorGUILayout.IntSlider(Rect(3, 3, position.width - 6, 15), "progress:", _progress, 0, 100);
        //         }
        //         yield return null;
        //     }
        // }
        public static void worker_WorkCompleted(bool cancelled)
        {
            Action action = () =>
            {
                string messeg = cancelled ? "Процесс отменён" : "Процесс завершён!";
                Debug.Log("messeg=" + messeg);
                pressedbool = false;
            };
            action();
            // if (InvokeRequired)
            //     Invoke(action);
            // else
            //     action();
        }
        public static void worker_ProcessChanged(int progress)
        {
            Action action = () => { _progress = progress; };
            // if (InvokeRequired)
            //     Invoke(action);
            // else
            //     action();
            action();
        }
        void OnInspectorUpdate()
        {
            Repaint();
        }
        ...
    }
}

Worker

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.Threading;
public class Worker
{
    public static bool _cancelled = false;
    public static void Cancel()
    {
        _cancelled = true;
    }
    public void Work()
    {
        for (int i = 0; i <= 100; i++)
        {
            if (_cancelled)
                break;

            Thread.Sleep(50);
            Debug.Log("i=" + i);
            marijnz.EditorCoroutines.PlacementObjects.worker_ProcessChanged(i);
        }
        marijnz.EditorCoroutines.PlacementObjects.worker_WorkCompleted(_cancelled);
    }
    public event Action<int> ProcessChanged;
    public event Action<bool> WorkCompleted;
}