با مجموعهها، منظم بمانید
ذخیره و طبقهبندی محتوا براساس اولویتهای شما.
شما می توانید از Firebase Authentication استفاده کنید تا به کاربران اجازه دهید با استفاده از یک یا چند روش ورود به سیستم وارد بازی شما شوند، از جمله ورود به سیستم آدرس ایمیل و رمز عبور، و ارائه دهندگان هویت فدرال مانند Google Sign-in و Facebook Login. این آموزش شما را با Firebase Authentication شروع می کند و به شما نشان می دهد که چگونه آدرس ایمیل و ورود رمز عبور را به بازی خود اضافه کنید.
پروژه Unity خود را ثبت کرده و آن را برای استفاده از Firebase پیکربندی کنید.
اگر پروژه Unity شما قبلاً از Firebase استفاده میکند، پس از قبل برای Firebase ثبت و پیکربندی شده است.
اگر پروژه یونیتی ندارید، می توانید یک برنامه نمونه دانلود کنید.
FirebaseUnity SDK (مخصوصا FirebaseAuth.unitypackage ) را به پروژه Unity خود اضافه کنید.
توجه داشته باشید که افزودن Firebase به پروژه Unity شما شامل وظایفی در کنسول Firebase و پروژه Unity باز شما می شود (به عنوان مثال، فایل های پیکربندی Firebase را از کنسول دانلود می کنید، سپس آنها را به پروژه Unity خود منتقل می کنید).
ثبت نام کاربران جدید
فرمی ایجاد کنید که به کاربران جدید امکان می دهد با استفاده از آدرس ایمیل و رمز عبور در بازی شما ثبت نام کنند. هنگامی که کاربر فرم را تکمیل کرد، آدرس ایمیل و رمز عبور ارائه شده توسط کاربر را تأیید کنید، سپس آنها را به روش CreateUserWithEmailAndPasswordAsync ارسال کنید:
auth.CreateUserWithEmailAndPasswordAsync(email,password).ContinueWith(task=>{if(task.IsCanceled){Debug.LogError("CreateUserWithEmailAndPasswordAsync was canceled.");return;}if(task.IsFaulted){Debug.LogError("CreateUserWithEmailAndPasswordAsync encountered an error: "+task.Exception);return;}// Firebase user has been created.Firebase.Auth.AuthResultresult=task.Result;Debug.LogFormat("Firebase user created successfully: {0} ({1})",result.User.DisplayName,result.User.UserId);});
ورود کاربران موجود
فرمی ایجاد کنید که به کاربران موجود اجازه دهد با استفاده از آدرس ایمیل و رمز عبور خود وارد سیستم شوند. وقتی کاربر فرم را تکمیل کرد، روش SignInWithEmailAndPasswordAsync فراخوانی کنید:
auth.SignInWithEmailAndPasswordAsync(email,password).ContinueWith(task=>{if(task.IsCanceled){Debug.LogError("SignInWithEmailAndPasswordAsync was canceled.");return;}if(task.IsFaulted){Debug.LogError("SignInWithEmailAndPasswordAsync encountered an error: "+task.Exception);return;}Firebase.Auth.AuthResultresult=task.Result;Debug.LogFormat("User signed in successfully: {0} ({1})",result.User.DisplayName,result.User.UserId);});
یک کنترل کننده رویداد تغییر وضعیت احراز هویت را تنظیم کنید و داده های کاربر را دریافت کنید
برای پاسخ به رویدادهای ورود به سیستم و خروج از سیستم، یک کنترل کننده رویداد را به شی احراز هویت جهانی متصل کنید. هر زمان که وضعیت ورود به سیستم کاربر تغییر کند، این کنترل کننده فراخوانی می شود. از آنجا که کنترل کننده تنها پس از اینکه شیء احراز هویت به طور کامل مقداردهی اولیه شود و پس از تکمیل تماس های شبکه اجرا می شود، بهترین مکان برای دریافت اطلاعات در مورد کاربر وارد شده است.
کنترل کننده رویداد را با استفاده از فیلد StateChanged شی FirebaseAuth ثبت کنید. هنگامی که کاربر با موفقیت وارد سیستم می شود، می توانید اطلاعات مربوط به کاربر را در کنترل کننده رویداد دریافت کنید.
در نهایت، زمانی که این شیء دارای Destroy بر روی آن باشد، به طور خودکار OnDestroy فراخوانی می کند. ارجاعات شیء Auth را در OnDestroy پاک کنید.
voidInitializeFirebase(){auth=Firebase.Auth.FirebaseAuth.DefaultInstance;auth.StateChanged+=AuthStateChanged;AuthStateChanged(this,null);}voidAuthStateChanged(objectsender,System.EventArgseventArgs){if(auth.CurrentUser!=user){boolsignedIn=user!=auth.CurrentUser && auth.CurrentUser!=null && auth.CurrentUser.IsValid();if(!signedIn && user!=null){DebugLog("Signed out "+user.UserId);}user=auth.CurrentUser;if(signedIn){DebugLog("Signed in "+user.UserId);displayName=user.DisplayName??"";emailAddress=user.Email??"";photoUrl=user.PhotoUrl??"";}}}voidOnDestroy(){auth.StateChanged-=AuthStateChanged;auth=null;}
مراحل بعدی
با نحوه افزودن پشتیبانی برای سایر ارائه دهندگان هویت و حساب های مهمان ناشناس آشنا شوید:
تاریخ آخرین بهروزرسانی 2025-09-03 بهوقت ساعت هماهنگ جهانی.
[[["درک آسان","easyToUnderstand","thumb-up"],["مشکلم را برطرف کرد","solvedMyProblem","thumb-up"],["غیره","otherUp","thumb-up"]],[["اطلاعاتی که نیاز دارم وجود ندارد","missingTheInformationINeed","thumb-down"],["بیشازحد پیچیده/ مراحل بسیار زیاد","tooComplicatedTooManySteps","thumb-down"],["قدیمی","outOfDate","thumb-down"],["مشکل ترجمه","translationIssue","thumb-down"],["مشکل کد / نمونهها","samplesCodeIssue","thumb-down"],["غیره","otherDown","thumb-down"]],["تاریخ آخرین بهروزرسانی 2025-09-03 بهوقت ساعت هماهنگ جهانی."],[],[],null,["You can use Firebase Authentication to allow users to sign in to your game using one\nor more sign-in methods, including email address and password sign-in, and\nfederated identity providers such as Google Sign-in and Facebook Login. This\ntutorial gets you started with Firebase Authentication by showing you how to add\nemail address and password sign-in to your game.\n\nBefore you begin\n\nBefore you can use\n[Firebase Authentication](/docs/reference/unity/namespace/firebase/auth),\nyou need to:\n\n- Register your Unity project and configure it to use Firebase.\n\n - If your Unity project already uses Firebase, then it's already\n registered and configured for Firebase.\n\n - If you don't have a Unity project, you can download a\n [sample app](//github.com/google/mechahamster).\n\n- Add the [Firebase Unity SDK](/download/unity) (specifically, `FirebaseAuth.unitypackage`) to\n your Unity project.\n\n| **Find detailed instructions for these initial\n| setup tasks in\n| [Add Firebase to your Unity project](/docs/unity/setup#prerequisites).**\n\nNote that adding Firebase to your Unity project involves tasks both in the\n[Firebase console](//console.firebase.google.com/) and in your open Unity project\n(for example, you download Firebase config files from the console, then move\nthem into your Unity project).\n\nSign up new users\n\nCreate a form that allows new users to register with your game using their email\naddress and a password. When a user completes the form, validate the email\naddress and password provided by the user, then pass them to the\n`CreateUserWithEmailAndPasswordAsync` method: \n\n auth.CreateUserWithEmailAndPasswordAsync(email, password).ContinueWith(task =\u003e {\n if (task.IsCanceled) {\n Debug.LogError(\"CreateUserWithEmailAndPasswordAsync was canceled.\");\n return;\n }\n if (task.IsFaulted) {\n Debug.LogError(\"CreateUserWithEmailAndPasswordAsync encountered an error: \" + task.Exception);\n return;\n }\n\n // Firebase user has been created.\n Firebase.Auth.AuthResult result = task.Result;\n Debug.LogFormat(\"Firebase user created successfully: {0} ({1})\",\n result.User.DisplayName, result.User.UserId);\n });\n\nSign in existing users\n\nCreate a form that allows existing users to sign in using their email address\nand password. When a user completes the form, call the\n`SignInWithEmailAndPasswordAsync` method: \n\n auth.SignInWithEmailAndPasswordAsync(email, password).ContinueWith(task =\u003e {\n if (task.IsCanceled) {\n Debug.LogError(\"SignInWithEmailAndPasswordAsync was canceled.\");\n return;\n }\n if (task.IsFaulted) {\n Debug.LogError(\"SignInWithEmailAndPasswordAsync encountered an error: \" + task.Exception);\n return;\n }\n\n Firebase.Auth.AuthResult result = task.Result;\n Debug.LogFormat(\"User signed in successfully: {0} ({1})\",\n result.User.DisplayName, result.User.UserId);\n });\n\nSet an authentication state change event handler and get user data\n\nTo respond to sign-in and sign-out events, attach an event handler to the global\nauthentication object. This handler gets called whenever the user's sign-in\nstate changes. Because the handler runs only after the authentication object is\nfully initialized and after any network calls have completed, it is the best\nplace to get information about the signed-in user.\n\nRegister the event handler using the `FirebaseAuth` object's `StateChanged`\nfield. When a user successfully signs in, you can get information about the user\nin the event handler.\n\nFinally, when this object has `Destroy` called on it, it will automatically call\n`OnDestroy`. Clean up the Auth object's references in `OnDestroy`. \n\n void InitializeFirebase() {\n auth = Firebase.Auth.FirebaseAuth.DefaultInstance;\n auth.StateChanged += AuthStateChanged;\n AuthStateChanged(this, null);\n }\n\n void AuthStateChanged(object sender, System.EventArgs eventArgs) {\n if (auth.CurrentUser != user) {\n bool signedIn = user != auth.CurrentUser && auth.CurrentUser != null\n && auth.CurrentUser.IsValid();\n if (!signedIn && user != null) {\n DebugLog(\"Signed out \" + user.UserId);\n }\n user = auth.CurrentUser;\n if (signedIn) {\n DebugLog(\"Signed in \" + user.UserId);\n displayName = user.DisplayName ?? \"\";\n emailAddress = user.Email ?? \"\";\n photoUrl = user.PhotoUrl ?? \"\";\n }\n }\n }\n\n void OnDestroy() {\n auth.StateChanged -= AuthStateChanged;\n auth = null;\n }\n\nNext steps\n\nLearn how to add support for other identity providers and anonymous guest\naccounts:\n\n- [Google Sign-in](/docs/auth/unity/google-signin)\n- [Facebook Login](/docs/auth/unity/facebook-login)\n- [Twitter Login](/docs/auth/unity/twitter-login)\n- [GitHub Login](/docs/auth/unity/github-auth)\n- [Microsoft Login](/docs/auth/cpp/microsoft-oauth)\n- [Yahoo Login](/docs/auth/cpp/yahoo-oauth)\n- [Anonymous sign-in](/docs/auth/unity/anonymous-auth)\n- [Phone Authentication](/docs/auth/unity/phone-auth)"]]