/// <summary>
        /// Call initialization function on the main thread only when the condition is met.
        /// </summary>
        /// <param name="condition">When it returns true, call the initializer once.
        /// If null, initializer will be called in the next editor update.</param>
        /// <param name="initializer">Initialization function to be called when condition is met.
        /// </params>
        /// <param name="name">Name of the component to be initialized, for debug purpose.</param>
        /// <param name="logger">Logger to be used to report when initialization is finished.</param>
        public static void InitializeOnMainThread(
            Func <bool> condition, Func <bool> initializer, string name, Logger logger = null)
        {
            if (initializer == null)
            {
                return;
            }

            // Cache the flag to prevent string comparison in every frame during
            // PollOnUpdateUntilComplete()
            bool isExecuteMethodEnabled = ExecutionEnvironment.ExecuteMethodEnabled;

            // Delay initialization until condition is met.
            RunOnMainThread.PollOnUpdateUntilComplete(() => {
                if (condition != null && !condition())
                {
                    // If Unity is launched with -executeMethod, in some Unity versions, editor
                    // update will never be called. As a result, PollOnUpdateUntilComplete() will
                    // attempt to call this poll function repeating on current thread until it returns
                    // true.  Therefore, return true immediately and stop the polling in executeMethod
                    // mode.
                    return(isExecuteMethodEnabled);
                }
                bool result = false;

                try {
                    result = initializer();
                } catch (Exception e) {
                    string errorMsg = String.Format("Exception thrown when initializing {0}: {1}",
                                                    name, e.ToString());
                    if (logger != null)
                    {
                        logger.Log(errorMsg, level: LogLevel.Error);
                    }
                    else
                    {
                        Debug.LogError(errorMsg);
                    }
                }

                if (logger != null)
                {
                    logger.Log(String.Format("{0} initialization {1}", name,
                                             result ? "succeeded." : "failed."),
                               level: result ? LogLevel.Verbose : LogLevel.Error);
                }

                return(true);
            });
        }
示例#2
0
            /// <summary>
            /// Schedule a job which is executed after the specified delay.
            /// </summary>
            /// <param name="job">Action to execute.</param>
            /// <param name="delayInMilliseconds">Time to wait for execution of this job.</param>
            /// <returns>ID of the scheduled job (always non-zero).</returns>
            public static int Schedule(Action job, double delayInMilliseconds)
            {
                ScheduledJob scheduledJob;

                lock (scheduledJobs) {
                    scheduledJob = new ScheduledJob {
                        Job   = job,
                        JobId = nextJobId,
                        DelayInMilliseconds = ExecutionEnvironment.ExecuteMethodEnabled ? 0.0 :
                                              delayInMilliseconds
                    };
                    scheduledJobs[nextJobId++] = scheduledJob;
                    if (nextJobId == 0)
                    {
                        nextJobId++;
                    }
                }
                RunOnMainThread.PollOnUpdateUntilComplete(scheduledJob.PollUntilExecutionTime);
                return(scheduledJob.JobId);
            }
示例#3
0
            public static int Schedule(Action job, double delayInMilliseconds)
            {
                object obj = RunOnMainThread.ScheduledJob.scheduledJobs;

                RunOnMainThread.ScheduledJob scheduledJob;
                lock (obj)
                {
                    scheduledJob = new RunOnMainThread.ScheduledJob
                    {
                        Job   = job,
                        JobId = RunOnMainThread.ScheduledJob.nextJobId,
                        DelayInMilliseconds = (!ExecutionEnvironment.InBatchMode) ? delayInMilliseconds : 0.0
                    };
                    RunOnMainThread.ScheduledJob.scheduledJobs[RunOnMainThread.ScheduledJob.nextJobId++] = scheduledJob;
                    if (RunOnMainThread.ScheduledJob.nextJobId == 0)
                    {
                        RunOnMainThread.ScheduledJob.nextJobId++;
                    }
                }
                RunOnMainThread.PollOnUpdateUntilComplete(new Func <bool>(scheduledJob.PollUntilExecutionTime));
                return(scheduledJob.JobId);
            }