Example #1
0
        private void OnEnable()
        {
            _target = (TrafficPath)target;
            so      = serializedObject;

            so.Update();

            var nodesProp = so.FindProperty("nodes");

            while (nodesProp.arraySize < 2)
            {
                nodesProp.InsertArrayElementAtIndex(nodesProp.arraySize == 0 ? 0 : nodesProp.arraySize - 1);
                nodesProp.GetArrayElementAtIndex(nodesProp.arraySize - 1).vector3Value = _target.transform.position + Vector3.one;
            }

            //disable add multiple
            so.FindProperty("addMultiple").boolValue = false;

            so.ApplyModifiedProperties();


            labelStyle                  = new GUIStyle();
            labelStyle.fontStyle        = FontStyle.Bold;
            labelStyle.normal.textColor = Color.white;
            labelStyle.fontSize         = 16;

            combinedPaths = new List <TrafficPath>();
        }
        private float GetBakedTime(TrafficPath path)
        {
            float    speed    = path.bakedResolution;
            GoSpline spline   = path.Spline();
            float    duration = spline.pathLength;

            return(fixedTime / (duration / (speed * 0.44704f)));
        }
        //Worker thread to do the baking
        IEnumerator Worker(TrafficPath path)
        {
            Debug.Log("Start Worker");
            //open folder to create new file
            //System.IO.FileStream fstream = Ultilities.OpenFile(path.gameObject.name, System.IO.FileMode.OpenOrCreate);

            string workerName = "Worker: " + path.gameObject.name;

            GameObject trafficBaker = new GameObject(workerName);

            trafficBaker.transform.SetParent(transform);

            float    progress = 0;
            GoSpline spline   = path.Spline();
            float    speed    = path.bakedResolution;
            float    duration = path.Spline().pathLength;

            List <Vector3> pathNodes = new List <Vector3>();
            string         pathName  = path.gameObject.name;

            //baking
            while (progress <= 1.0f)
            {
                yield return(new WaitForFixedUpdate());

                progress += Time.fixedDeltaTime / (duration / (speed * 0.44704f));
                Vector3 pos = spline.getPointOnPath(progress);
                trafficBaker.transform.position = pos;
                pathNodes.Add(pos);
                trafficBaker.name = workerName + ": " + progress * 100 + "%";
            }
            //done baking

            Debug.Log(workerName + ": Done");
            Debug.Log("Nodes Count: " + pathNodes.Count);

            /*
             * BakedTrafficPath bakedPath = ScriptableObject.CreateInstance<BakedTrafficPath>();
             * bakedPath.Init(pathNodes, pathName, path.pathType, path.pathSpeedMPH, path.bakedResolution, path.splitChance, path.smartTraffic, path.phaseTypes, path.notes);
             * bakedPath.CreateAndSave(savedLocation);
             */
            yield return(null);
        }
        private void MonitorStatusRoutine()
        {
            int aliveCount = 0;

            for (int i = 0; i < pathInfos.Count; i++)
            {
                if (pathInfos[i].threadHandle.IsAlive)
                {
                    aliveCount++;
                }
            }

            if (aliveCount == 0)
            {
                Debug.Log("Baking Done");
                //all done
                //write data to asset

                for (int i = 0; i < pathInfos.Count; i++)
                {
                    if (pathInfos[i].bakedNodes == null)
                    {
                        continue;
                    }
                    TrafficPath path = pathInfos[i].path;
                    Debug.Log(pathInfos[i].bakedNodes.Count);
                    BakedTrafficPath bakedPath = ScriptableObject.CreateInstance <BakedTrafficPath>();
                    bakedPath.Init(pathInfos[i].bakedNodes, path.gameObject.name, path.type, path.pathSpeedMPH, path.bakedResolution, path.splitChance, path.notes);
                    bakedPath.CreateAndSave(serializedObject.FindProperty("savedLocation").stringValue);
                    pathInfos[i].bakedNodes = null;
                    return;
                }

                //reset
                EditorApplication.update -= MonitorStatusRoutine;
                status = BakeStatus.Preparing;
            }
        }