العد التنازلي البرنامج التعليمي للوحدة

مؤقت العد التنازلي عبارة عن ساعة افتراضية يتم حسابها من وقت محدد حتى 0.

لإنشاء مؤقت للعد التنازلي في Unity، ستحتاج إلى إنشاء برنامج نصي يخزن مقدار الوقت الذي سيتم العد التنازلي فيه وسيعرضه بتنسيق 00:00.

الوحدة للعد التنازلي الزاوية العلوية اليسرى.

سيحتوي المؤقت على هذه التنسيقات:

  • الأيام:الساعات:الدقائق:الثواني:ميلي ثانية
  • الساعات: الدقائق: الثواني: ميلي ثانية
  • الدقائق: الثواني: ميلي ثانية
  • الثواني: ميلي ثانية
  • بالإضافة إلى كل ما سبق ولكن بدون ميلي ثانية

خطوات

لعمل مؤقت للعد التنازلي في Unity، اتبع الخطوات التالية:

  • قم بإنشاء سكريبت جديد، وسمه 'SC_CountdownTimer'، وأزل كل شيء منه ثم الصق الكود أدناه:
  • سيتم خصم البرنامج النصي لمؤقت العد التنازلي C# من القيمة الإجمالية حتى الوصول إلى 0 وسيطبق الوقت المنسق على عنصر النص.

SC_CountdownTimer.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class SC_CountdownTimer : MonoBehaviour
{
    public enum CountdownFormatting { DaysHoursMinutesSeconds, HoursMinutesSeconds, MinutesSeconds, Seconds };
    public CountdownFormatting countdownFormatting = CountdownFormatting.MinutesSeconds; //Controls the way the timer string will be formatted
    public bool showMilliseconds = true; //Whether to show milliseconds in countdown formatting
    public double countdownTime = 600; //Countdown time in seconds

    Text countdownText;
    double countdownInternal;
    bool countdownOver = false;

    // Start is called before the first frame update
    void Start()
    {
        countdownText = GetComponent<Text>();
        countdownInternal = countdownTime; //Initialize countdown
    }

    void FixedUpdate()
    {
        if (countdownInternal > 0)
        {
            countdownInternal -= Time.deltaTime;

            //Clamp the timer value so it never goes below 0
            if (countdownInternal < 0)
            {
                countdownInternal = 0;
            }

            countdownText.text = FormatTime(countdownInternal, countdownFormatting, showMilliseconds);
        }
        else
        {
            if (!countdownOver)
            {
                countdownOver = true;

                Debug.Log("Countdown has finished running...");

                //Your code here...
            }
        }
    }

    string FormatTime(double time, CountdownFormatting formatting, bool includeMilliseconds)
    {
        string timeText = "";

        int intTime = (int)time;
        int days = intTime / 86400;
        int hoursTotal = intTime / 3600;
        int hoursFormatted = hoursTotal % 24;
        int minutesTotal = intTime / 60;
        int minutesFormatted = minutesTotal % 60;
        int secondsTotal = intTime;
        int secondsFormatted = intTime % 60;
        int milliseconds = (int)(time * 100);
        milliseconds = milliseconds % 100;

        if (includeMilliseconds)
        {
            if (formatting == CountdownFormatting.DaysHoursMinutesSeconds)
            {
                timeText = string.Format("{0:00}:{1:00}:{2:00}:{3:00}:{4:00}", days, hoursFormatted, minutesFormatted, secondsFormatted, milliseconds);
            }
            else if (formatting == CountdownFormatting.HoursMinutesSeconds)
            {
                timeText = string.Format("{0:00}:{1:00}:{2:00}:{3:00}", hoursTotal, minutesFormatted, secondsFormatted, milliseconds);
            }
            else if (formatting == CountdownFormatting.MinutesSeconds)
            {
                timeText = string.Format("{0:00}:{1:00}:{2:00}", minutesTotal, secondsFormatted, milliseconds);
            }
            else if (formatting == CountdownFormatting.Seconds)
            {
                timeText = string.Format("{0:00}:{1:00}", secondsTotal, milliseconds);
            }
        }
        else
        {
            if (formatting == CountdownFormatting.DaysHoursMinutesSeconds)
            {
                timeText = string.Format("{0:00}:{1:00}:{2:00}:{3:00}", days, hoursFormatted, minutesFormatted, secondsFormatted);
            }
            else if (formatting == CountdownFormatting.HoursMinutesSeconds)
            {
                timeText = string.Format("{0:00}:{1:00}:{2:00}", hoursTotal, minutesFormatted, secondsFormatted);
            }
            else if (formatting == CountdownFormatting.MinutesSeconds)
            {
                timeText = string.Format("{0:00}:{1:00}", minutesTotal, secondsFormatted);
            }
            else if (formatting == CountdownFormatting.Seconds)
            {
                timeText = string.Format("{0:00}", secondsTotal);
            }
        }

        return timeText;
    }
}
  • أنشئ نصًا جديدًا لواجهة المستخدم، من خلال النقر بزر الماوس الأيمن على عرض التسلسل الهرمي -> واجهة المستخدم -> النص وتسميته 'Countdown'

تقوم الوحدة بإنشاء نص جديد لواجهة المستخدم

  • تغيير 'Countdown' محاذاة تحويل المستطيل إلى أعلى اليسار، والمحور إلى (0، 1)، وPos X وPos Y إلى 5، والعرض إلى 300، والارتفاع إلى 60

  • قم بتغيير نمط خط النص 'Countdown' إلى غامق، وحجم الخط إلى 34، والمحاذاة إلى الوسط الأيسر، واللون إلى الأبيض

وحدة مفتش مكون النص Arial Bold حجم الخط 34

  • قم بإرفاق البرنامج النصي SC_CountdownTimer بالكائن 'Countdown' الذي يحتوي على مكون نص.

ستلاحظ أن البرنامج النصي يحتوي على بعض المتغيرات:

  • تنسيق العد التنازلي يتحكم في وحدات الوقت التي سيتم تضمينها في تنسيق السلسلة.
  • إظهار عناصر التحكم بالميلي ثانية إذا كان يجب إظهار عدد المللي ثانية.
  • وقت العد التنازلي هو مدة العد التنازلي بالثواني، على سبيل المثال، القيمة 600 تقابل 10 دقائق.

بعد الضغط على Play يجب أن تلاحظ النص الذي تم ملؤه بمؤقت العد التنازلي:

في 0 ثانية، سيطبع البرنامج النصي سطرًا في وحدة التحكم، للإشارة إلى انتهاء العد التنازلي، استخدم هذا الجزء من البرنامج النصي لإضافة وظائفك الخاصة.

المقالات المقترحة
البرنامج النصي لإنشاء مفتاح الضوء في الوحدة
الماوس نظرة السيناريو للوحدة
كيفية استخدام وحدة تحكم Xbox في الوحدة
Raycast ونص إطلاق النار القائم على المقذوفات من أجل الوحدة
كيفية إعداد جهاز التحكم بعصا التحكم للحركة في الوحدة
البرنامج التعليمي للقطة شاشة الوحدة
اختيار وحدة نمط RTS للوحدة