コード例 #1
0
    private void InitializeOnce()
    {
        // The agent will be disabled on any error.

        if (!enabled || NavManager.ActiveManager == null)
        {
            Debug.LogError(name + ": Failed navigation initialization. Missing resources or bad"
                           + " configuration. Agent forced to disabled state.");

            enabled = false;
            return;
        }

        mManager = NavManager.ActiveManager;

        mNav = mManager.CreateAgent(agentGroup, transform);

        if (mNav == null)
        {
            Debug.LogError(name + ": Failed navigation initialization. Could not get navigation"
                           + " agent from manager. Agent forced to disabled state.");

            enabled = false;
            return;
        }

        mNav.data.rotation = transform.rotation;
        mNav.data.position = mNav.GetPointSearch(transform.position);

        if (mNav.data.position.polyRef == 0)
        {
            mNav = null;
            Debug.LogError(name + ": Failed to constrain position to the navigation mesh."
                           + " Agent forced to disabled state.");

            enabled = false;
            return;
        }

        IState planner = new NavPlanner(mNav);
        IState mover   = new SimpleMover(mNav);

        mManagerIndex = mManager.AddAgent(planner, mover);
        if (mManagerIndex == -1)
        {
            Debug.LogError(name +
                           ": Could not add agent to navigation manager. Agent forced to disabled state.");

            enabled = false;
            return;
        }

        context = new AgentContext(this.gameObject, new NavController(mNav));

        mBrain = GetBrain();

        if (mBrain == null)
        {
            mManager.RemoveAgent(mManagerIndex);

            Debug.LogError(
                name + ": Could not get the agent brain. Agent forced to disabled state.");

            enabled = false;
            return;
        }
    }